Replies: 25 comments 59 replies
-
这里是不是少讲了函数指针,fn类型 |
Beta Was this translation helpful? Give feedback.
-
pub struct Rectangle { impl Rectangle { fn main() {
} 这个位置为什么说 rect1.width 是私有的呢? |
Beta Was this translation helpful? Give feedback.
-
只要是定义在impl中的函数就叫关联函数吧?而不管有没有self参数 |
Beta Was this translation helpful? Give feedback.
-
关联函数的描述,‘Rust 中有一个约定俗称的规则’,应该是约定俗成 |
Beta Was this translation helpful? Give feedback.
-
这个枚举类型好像Python里的类属性啊,或者 impl Rectangle { //为Rectangle结构体增加一种方法
fn can_hold(self, other: Rectangle) -> bool {
if (self.length >= other.length && self.width >= other.width)||(self.length >= other.width && self.width >= other.length) {
return true
}
false
}
} |
Beta Was this translation helpful? Give feedback.
-
let rect3 = Rectangle { width: 60, height: 45 }; 这两种写法是等价的吗? |
Beta Was this translation helpful? Give feedback.
-
”Rust 中有一个约定俗成的规则,使用 new 来作为构造器的名称,出于设计上的考虑,Rust 特地没有用 new 作为关键字“ |
Beta Was this translation helpful? Give feedback.
-
关联函数和方法的区别是啥? |
Beta Was this translation helpful? Give feedback.
-
ref rustdoc std::ops |
Beta Was this translation helpful? Give feedback.
-
这章两个概念比较绕,关联函数和方法的区别。这里做个笔记: |
Beta Was this translation helpful? Give feedback.
-
关联函数,就叫构造函数又怎么了嘛,非要取些怪头怪脑的名字让人不便记忆 |
Beta Was this translation helpful? Give feedback.
-
关联函数比构造函数更灵活,还不用考虑c++中一系列的继承析构问题; |
Beta Was this translation helpful? Give feedback.
-
我理解 ‘&self’相当于java中的this |
Beta Was this translation helpful? Give feedback.
-
如果理解正确,对比C++,关联函数类似 |
Beta Was this translation helpful? Give feedback.
-
教程中应该加入一些其他语言的类比,帮助加快理解速度。如果只是扣文字和一些简单案例,效率低,还容易造成困惑。 |
Beta Was this translation helpful? Give feedback.
-
Rust 会自动为 object 添加 &、&mut 或 * 以便使 object 与方法签名匹配。 这句话是错误的,&mut这个不会加,会报错啊!在实现了copy trait的时候 好像又莫名其妙的能调用了,但是前提条件却没有 |
Beta Was this translation helpful? Give feedback.
-
所以自由函数、关联函数、方法这三者在底层实现上是怎样的?在运行和性能上有差异吗? 我的理解是,
然而,Rust有个很灵活的特性:类型实例的 方法 实际上也可以通过 关联函数 的形式进行调用, // 定义所用的结构体
struct S(i32);
// 这是结构体的 实例方法
impl S {
pub fn add_from(&mut self, other: i32) {
self.0 += other;
}
}
// 与之等价的 自由函数
pub fn add_from(s: &mut S, other: i32) {
s.0 += other;
}
fn main() {
// 初始化一个结构体
let mut s = S(0);
// 以 实例方法 的形式调用
s.add_from(1);
// 以 关联函数 的形式调用
S::add_from(&mut s, 1);
// 用 自由函数 调用
add_from(&mut s, 1);
} 这里边的 关联函数 个人觉得已经很像 自由函数 了(除了加个类型作为命名空间区分), |
Beta Was this translation helpful? Give feedback.
-
// 可以为枚举实现方法
enum Message{
Quit {boolean : bool},
Move {x : i32, y : i32},
Write(String),
ChangeColor(i32, i32, i32)
}
impl Message{
fn call(&self) -> (){
// 输出write里的string
match self {
Message::Write(string) => {
println!("Message string is {}", string)
},
Message::ChangeColor(x, y , z) => {
println!("Message change color is ({}, {}, {})", x, y, z)
}
_ => ()
}
}
}
let msg1 = Message::Write("Hello".to_string());
let msg2 = Message::ChangeColor(1,2,3);
msg1.call();
msg2.call(); |
Beta Was this translation helpful? Give feedback.
-
Trait 其他教材翻译成特型, 这里翻译成特征. 感觉怪怪的 |
Beta Was this translation helpful? Give feedback.
-
是不是我还没学到难点,至少目前觉得还挺简单的😉 |
Beta Was this translation helpful? Give feedback.
-
这章类比其他语言的静态方法、实例方法学习比较容易理解~ |
Beta Was this translation helpful? Give feedback.
-
其实 |
Beta Was this translation helpful? Give feedback.
-
https://course.rs/basic/method.html
Beta Was this translation helpful? Give feedback.
All reactions