Rust中方法和关联函数的区别
1.定义方式不同方法定义:// self或&selffn 方法名(&self, 参数)->返回值{ // 方法体}关联函数定义:fn 函数名(参数) 返回值{ // 函数体}// 例fn new(x:i32, y:i32) -> Rectangle{ Rectangle { x: x, y: y, }}2.调用方式不同方法调用,使用“.” ,例:rect1.area()关联函数调用,使用“::”,类似其他语言的静态方法,例:l
原创
2022-03-16 20:38:29 ·
662 阅读 ·
0 评论