update at 2020-09-11 18:13:07 by ehlxr

This commit is contained in:
ehlxr 2020-09-11 18:13:07 +08:00
parent f0b64dd81d
commit 39bb6127de

View File

@ -811,7 +811,7 @@ fn main() {
let mut v = vec![1, 2, 3, 4, 5, 6]; let mut v = vec![1, 2, 3, 4, 5, 6];
let r = &mut v[..]; let r = &mut v[..];
// let (a, b) = r.split_at_mut(3); let (_a, _b) = r.split_at_mut(3);
let (a, b) = split_at_mut(r, 3); let (a, b) = split_at_mut(r, 3);
println!("{:?}", a); println!("{:?}", a);
println!("{:?}", b); println!("{:?}", b);
@ -881,7 +881,7 @@ fn main() {
impl Red for Ball<'_> {} impl Red for Ball<'_> {}
let num = 5; let num = 5;
let obj = Box::new(Ball { diameter: &num }) as Box<dyn Red>; let _obj = Box::new(Ball { diameter: &num }) as Box<dyn Red>;
// --------------------------- // ---------------------------
println!("---------------------------"); println!("---------------------------");
@ -922,7 +922,7 @@ fn main() {
fn returns_closure() -> Box<dyn Fn(i32) -> i32> { fn returns_closure() -> Box<dyn Fn(i32) -> i32> {
Box::new(|x| x + 1) Box::new(|x| x + 1)
} }
let rc = returns_closure(); let _rc = returns_closure();
// println!("{}", rc(12)); // println!("{}", rc(12));
// --------------------------- // ---------------------------
@ -1068,6 +1068,13 @@ fn main() {
let p1 = Point1 { x: "hello", y: 'd' }; let p1 = Point1 { x: "hello", y: 'd' };
let p2 = p.mixup(p1); let p2 = p.mixup(p1);
println!("{:?}", p2); println!("{:?}", p2);
println!("-------------完全限定语法与消歧义:调用相同名称的方法----------");
let person = Human;
Pilot::fly(&person);
Wizard::fly(&person);
// person.fly();
Human::fly(&person);
} }
#[derive(Debug)] #[derive(Debug)]
@ -1758,3 +1765,31 @@ mod tests {
assert_eq!(mock_messenger.sent_messages.borrow().len(), 1); assert_eq!(mock_messenger.sent_messages.borrow().len(), 1);
} }
} }
trait Pilot {
fn fly(&self);
}
trait Wizard {
fn fly(&self);
}
struct Human;
impl Pilot for Human {
fn fly(&self) {
println!("This is your captain speaking.");
}
}
impl Wizard for Human {
fn fly(&self) {
println!("Up!");
}
}
impl Human {
fn fly(&self) {
println!("*waving arms furiously*");
}
}