basic/trait/advance-trait #777
Replies: 68 comments 47 replies
-
有点难懂,要睇多两次。 |
Beta Was this translation helpful? Give feedback.
-
到这卡壳了,休息一下,改天再来 |
Beta Was this translation helpful? Give feedback.
-
哇这一节直接懵了 先跳过了 学会点集合操作刷刷力扣 把Rust用起来再说😂 |
Beta Was this translation helpful? Give feedback.
-
newtype 就是装箱,配合 Deref 自动从箱里拿出值 |
Beta Was this translation helpful? Give feedback.
-
特征定义中的特征约束,算不算是trait的继承? |
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.
-
#![allow(unused)] 这段code没有看明白。是指Address的实际类型需要同时实现后边的所有trait吗?也是trait限定? |
Beta Was this translation helpful? Give feedback.
-
修改默认泛型类型参数部分,完整参考代码: use std::ops::Add;
#[derive(Debug)]
struct Millimeters(u32);
struct Meters(u32);
impl Add<Meters> for Millimeters {
type Output = Millimeters;
fn add(self, other: Meters) -> Self::Output {
Millimeters(self.0 + (other.0 * 1000))
}
}
fn main() {
let x = Millimeters(5);
let y = Meters(3);
print!("{:?}", x.add(y));
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
好难好难 前面几节看了半天。 这一节就花了我半天时间, 关键是 还很懵逼 |
Beta Was this translation helpful? Give feedback.
-
所有self.0是取出元组结构体第一个字段的意思吗,我看了好久qaq |
Beta Was this translation helpful? Give feedback.
-
在默认类型参数那里 ,如果Point 是带泛型的 Point<T:Add>,那么如何给Point实现Add特征呢? |
Beta Was this translation helpful? Give feedback.
-
补充一点:关联类型不是为了简单简洁而存在的,associated type是有实际用处的: impl Trait<A> for X
impl Trait<B> for X
impl Trait<C> for X 但是有了关联类型我们可以限定trait的使用者只能为一个类型impl一个Trait: impl Trait for X // 只能为X实现唯一一个Trait::AssociatedType 这个用在迭代器里很合理,因为在rust里没有higher-kinded type |
Beta Was this translation helpful? Give feedback.
-
还有方法没有 |
Beta Was this translation helpful? Give feedback.
-
为Wrpper实现Deref特征: use std::fmt;
struct Wrapper(Vec<String>);
impl fmt::Display for Wrapper {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{}]", self.join(", "))
}
}
impl core::ops::Deref for Wrapper {
type Target = Vec<String>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
fn main() {
let w = Wrapper(vec![String::from("hello"), String::from("world")]);
println!("w = {}", w);
} |
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.
-
第一次插眼,懵逼ing |
Beta Was this translation helpful? Give feedback.
-
trait 用法太多了,需要自己重新总结一遍 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
感觉Rust的Trait融合了其他语言的多态、接口甚至装饰器之类的很多特性,是个相当复杂的大集合。我看了好几遍都是似懂非懂,关键是不清楚其中一些功能会在开发中如何使用,有什么好处。还是得有个具体的小项目练练手才行。 |
Beta Was this translation helpful? Give feedback.
-
最后为 Wrapper 实现 Deref 特征和重载方法怎么不讲了 |
Beta Was this translation helpful? Give feedback.
-
整个泛型和特征2.8章节读了3-4遍……记不住 |
Beta Was this translation helpful? Give feedback.
-
Rust 简直太自由辣 |
Beta Was this translation helpful? Give feedback.
-
basic/trait/advance-trait
https://course.rs/basic/trait/advance-trait.html
Beta Was this translation helpful? Give feedback.
All reactions