generics-traits/advanced-traits #200
Replies: 57 comments 29 replies
-
`impl<T: Sub<Output = T>> Sub for Point {
}` |
Beta Was this translation helpful? Give feedback.
-
最后一题 为么下面的结果还是11呢 |
Beta Was this translation helpful? Give feedback.
-
请问在第三题里面,为什么不使用完全限定语法也可以编译通过呢? assert_eq!(<Human as Pilot>::fly(&person), "This is your captain speaking.");
assert_eq!(Pilot::fly(&person), "This is your captain speaking.");
assert_eq!(<Human as Wizard>::fly(&person), "Up!");
assert_eq!(Wizard::fly(&person), "Up!"); |
Beta Was this translation helpful? Give feedback.
-
问个问题:第一题我的通过编译的解答是:
但我看参考答案里无论在trait中还是函数签名中都没有给出 std::ops::Sub 的约束,为什么参考答案也能编译通过?下面是参考答案的函数签名:
|
Beta Was this translation helpful? Give feedback.
-
done 我想问一下 |x| x+1这算什么运算? |
Beta Was this translation helpful? Give feedback.
-
done,这章主要讲了泛型与特征的相关知识,目前只能说是有了初步得理解,之后还需通过练习进行更深入得理解 |
Beta Was this translation helpful? Give feedback.
-
第四题没理解 另外减法的时候为什么必须顺序反过来?
···rust struct Foo; #[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)] // The
} impl ops::Sub for Foo {
} fn main() { |
Beta Was this translation helpful? Give feedback.
-
感谢大佬的工作,最近看的rust资料,找了几个不同的教程,这个电子书是最有帮助的,也解答我之前遇到很多的问题。 |
Beta Was this translation helpful? Give feedback.
-
最后一题不理解QAQ |
Beta Was this translation helpful? Give feedback.
-
这个评论跟习题对应不上还是小节里面的归总到一起了?另外 10.5的第四题 &self 签名,为什么不能String::from(self.name), 而是要self.name.clone() |
Beta Was this translation helpful? Give feedback.
-
破案了,10.3与10.5这两个练习题的评论合在一起了 |
Beta Was this translation helpful? Give feedback.
-
第11题 new的参数 (|x| x+1)是一个闭包,类似于python的lambda函数 |
Beta Was this translation helpful? Give feedback.
-
哎卧槽,把sheep看成了sleep,还以为大神有裸睡习惯。。。 |
Beta Was this translation helpful? Give feedback.
-
mark finished |
Beta Was this translation helpful? Give feedback.
-
mark finished |
Beta Was this translation helpful? Give feedback.
-
This exercise is quite tough could not do most of them , i need revisit again after some more practice of rust |
Beta Was this translation helpful? Give feedback.
-
第4题是不是有点歧义,按题目要求应该是只加派生注解,不改代码。 |
Beta Was this translation helpful? Give feedback.
-
Done ✅ |
Beta Was this translation helpful? Give feedback.
-
目前来说最难的一节,我竟然全部做对了 |
Beta Was this translation helpful? Give feedback.
-
第一题 struct Container(i32, i32);
// 使用关联类型实现重新实现以下特征
// trait Contains {
// type A;
// type B;
/*
trait Contains<A, B> {
fn contains(&self, _: &A, _: &B) -> bool;
fn first(&self) -> i32;
fn last(&self) -> i32;
}
*/
trait Contains {
type A;
type B;
fn contains(&self, _: &Self::A, _: &Self::B) -> bool;
fn first(&self) -> i32;
fn last(&self) -> i32;
}
/*impl Contains<i32, i32> for Container {
fn contains(&self, number_1: &i32, number_2: &i32) -> bool {
(&self.0 == number_1) && (&self.1 == number_2)
}
// Grab the first number.
fn first(&self) -> i32 { self.0 }
// Grab the last number.
fn last(&self) -> i32 { self.1 }
}*/
impl Contains for Container {
type A = i32;
type B = i32;
fn contains(&self, number_1: &Self::A, number_2: &Self::B) -> bool {
(&self.0 == number_1) && (&self.1 == number_2)
}
// Grab the first number.
fn first(&self) -> i32 { self.0 }
// Grab the last number.
fn last(&self) -> i32 { self.1 }
}
/*fn difference<A, B, C: Contains<A, B>>(container: &C) -> i32 {
container.last() - container.first()
}*/
fn difference(container: &Container) -> i32{
container.last() - container.first()
}
fn main() {
let number_1 = 3;
let number_2 = 10;
let container = Container(number_1, number_2);
println!("Does container contain {} and {}: {}",
&number_1, &number_2,
container.contains(&number_1, &number_2));
println!("First number: {}", container.first());
println!("Last number: {}", container.last());
println!("The difference is: {}", difference(&container));
} 第二题 impl Sub for Point<i32> {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
Point {
x: self.x - other.x,
y: self.y - other.y,
}
}
} impl <T: Sub<Output = T>>Sub for Point<T> {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
Point {
x: self.x - other.x,
y: self.y - other.y,
}
}
} impl Sub<Point<i32>> for Point<i32> {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
Point {
x: self.x - other.x,
y: self.y - other.y,
}
}
} 第三题 fn main() {
let person = Human;
assert_eq!(Pilot::fly(&person), "This is your captain speaking.");
assert_eq!(Wizard::fly(&person), "Up!");
assert_eq!(person.fly(), "*waving arms furiously*");
println!("Success!")
} 第四题 trait Person {
fn name(&self) -> String;
}
// Person 是 Student 的 supertrait .
// 实现 Student 需要同时实现 Person.
trait Student: Person {
fn university(&self) -> String;
}
trait Programmer {
fn fav_language(&self) -> String;
}
// CompSciStudent (computer science student) 是 Programmer
// 和 Student 的 subtrait. 实现 CompSciStudent 需要先实现这两个 supertraits.
trait CompSciStudent: Programmer + Student {
fn git_username(&self) -> String;
}
fn comp_sci_student_greeting(student: &dyn CompSciStudent) -> String {
format!(
"My name is {} and I attend {}. My favorite language is {}. My Git username is {}",
student.name(),
student.university(),
student.fav_language(),
student.git_username()
)
}
struct CSStudent {
name: String,
university: String,
fav_language: String,
git_username: String
}
impl Programmer for CSStudent {
fn fav_language(&self) -> String {
self.fav_language.clone()
}
}
impl Student for CSStudent {
fn university(&self) -> String {
self.university.clone()
}
}
impl Person for CSStudent {
fn name(&self) -> String {
self.name.clone()
}
}
// 为 CSStudent 实现所需的特征
impl CompSciStudent for CSStudent {
fn git_username(&self) -> String {
self.git_username.clone()
}
}
fn main() {
let student = CSStudent {
name: "Sunfei".to_string(),
university: "XXX".to_string(),
fav_language: "Rust".to_string(),
git_username: "sunface".to_string()
};
// 填空
println!("{}", comp_sci_student_greeting(&student));
} 第五题 use std::fmt;
// 定义一个 newtype `Pretty`
struct Pretty(String);
impl fmt::Display for Pretty {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\"{}\"", self.0.clone() + ", world")
}
}
fn main() {
let w = Pretty("hello".to_string());
println!("w = {}", w);
} |
Beta Was this translation helpful? Give feedback.
-
generics-traits/advanced-traits
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/generics-traits/advanced-traits.html
Beta Was this translation helpful? Give feedback.
All reactions