basic/trait/trait-object #685
Replies: 170 comments 129 replies
-
特征对象要划重点,得多温习 |
Beta Was this translation helpful? Give feedback.
-
各位大佬们,怎么理解 Box动态分发,就像例子中:
这里x, y不是已知类型么,为什么不能像静态分发一样,为u8和f64分别生成一份代码 |
Beta Was this translation helpful? Give feedback.
-
fn add<T: Add<T, Output = T>>(a: T, b: T) -> T 这个 Output = T 没看懂啊 |
Beta Was this translation helpful? Give feedback.
-
最后好像没有直接给出一开始提出问题的解决方法, 是在练习里吗? |
Beta Was this translation helpful? Give feedback.
-
觉得 Box 智能指针能在本节做一个介绍,可能会更方便大家理解本节内容 |
Beta Was this translation helpful? Give feedback.
-
特征对象,有点像java的接口类型,例如Map,List。例如: List<String> list = new ArrayList<String>(); List是一个接口,ArrayList类型实现了List接口。 |
Beta Was this translation helpful? Give feedback.
-
眼睛看懂了,手还没懂。练习题都不够撸。 |
Beta Was this translation helpful? Give feedback.
-
我好像知道了什么,留下了贫穷的泪水 |
Beta Was this translation helpful? Give feedback.
-
看到这里,表示这是我学习 Rust 的第一个卡点。。 |
Beta Was this translation helpful? Give feedback.
-
特征对象可以模拟继承 |
Beta Was this translation helpful? Give feedback.
-
其实就是“多态”,在不同的语言里面有不同的名称、语法和实现方式,比如c语言就是用不安全的指针粗暴地实现,c++用虚函数来实现,java通过抽象类与接口来实现,像go,swift这种很新的语言都倾向于通过更灵活的鸭式辨型的抽象来实现多态和动态派发,go里面叫interface,swift里面叫protocol,而rust里面就叫trait |
Beta Was this translation helpful? Give feedback.
-
为自定义类型实现 + 操作 给struct Point加 :Add<T,Output =T > 和 不加 有什么区别呢? 请大佬讲解下 |
Beta Was this translation helpful? Give feedback.
-
用下标处理,就不需要 fn largest<T: PartialOrd>(list: &[T]) -> &T {
let mut largest_idx = 0_usize;
for i in 0..list.len() {
if list[i] > list[largest_idx] {
largest_idx = i;
}
}
&list[largest_idx]
} |
Beta Was this translation helpful? Give feedback.
-
哎,太难了,先看后面的,回头再来啃 |
Beta Was this translation helpful? Give feedback.
-
fn largest<T: std::cmp::PartialOrd>(list: &[T]) -> &T {
} |
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.
-
学过java的泛型 看这节还是难度飙升 |
Beta Was this translation helpful? Give feedback.
-
大家看看对不对,我降维举例一下那个特征作为函数参数,只有带有某种特征的参数才能作为那个函数的输入,比如着函数的功能是地铁站检查液体是否能带进去,那个检测仪就是函数,你只有具备液体这个特征才能被检测。 |
Beta Was this translation helpful? Give feedback.
-
特针可以impl其他特征吗? |
Beta Was this translation helpful? Give feedback.
-
trait 特性,接触过大数据 scala 语言的人就听过这个名词,包括模式匹配,真的modern的现代语言才会有这些特性。trait 特性真得很帅 (比 OOP不知道高到哪里去了)。使用现代语言后,就懒得搞 java、c++ 那些啰嗦又臃肿的故纸堆了。 |
Beta Was this translation helpful? Give feedback.
-
”但是这种写法限制了 Screen 实例的 Vec 中的每个元素必须是 Button 类型或者全是 SelectBox 类型。“ 这句话中的:”必须是Button类型“应该改为:”必须全是Button类型“才对;这个细节错误让我看了半天才理解 |
Beta Was this translation helpful? Give feedback.
-
之前看官方文档不太清楚,这次完全理解了。 |
Beta Was this translation helpful? Give feedback.
-
impl trait 和 dyn trait,分别是静态分发和动态分发,前者在编译时已经知道数据类型,后者不知道类型,所以一般使用智能指针来wrap一下,智能指针的大小一般都是确定的。 |
Beta Was this translation helpful? Give feedback.
-
看完这部分感觉有点喜欢 rust 了🦀 |
Beta Was this translation helpful? Give feedback.
-
这不就是更加灵活的Java 的接口吗 |
Beta Was this translation helpful? Give feedback.
-
关于最后一个例子的部分代码impl Display for FileState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
FileState::Open => write!(f, "OPEN"),
FileState::Closed => write!(f, "CLOSED"),
}
}
} 这里为什么 |
Beta Was this translation helpful? Give feedback.
-
basic/trait/trait-object
https://course.rs/basic/trait/trait-object.html
Beta Was this translation helpful? Give feedback.
All reactions