• 万正宇
    2022-04-24
    --014 pattern match //fn sum(x: String, ref y: String) -> String { x + y } fn f(x: &Option<String>) { match x { Some(ref s) => { println!("{:?}", s)},//ref模式 None => { println!("none") } } } fn main() { let x = Some("ky-tech".to_owned()); f(&x); //匹配动态数组 let vec = vec![1, 2, 3]; match vec[..] { [a, b, c] => {println!("{}, {}, {}", a, b, c)}, _ => { println!("no match vector") }, }; } --015 DeRef Trait 类型的指针行为 Drop Trait 类型的析构行为 impl<T:?Sized> Deref for Box<T> { type Target = T; fn deref(&self) -> &T { &**self } } //// use std::ops::Deref; struct Point<T>(T); impl<T> Point<T> { fn new(x: T) -> Point<T> { Point(x) } } impl<T> Deref for Point<T> { type Target = T; fn deref(&self) -> &T { &self.0 } } fn main() { let y = Point::new(3); assert_eq!(3, *y); println!("*y = {:?}", *y); } --016 *y = *( y.deref() ) Box<T> Vec<T> String Rc<T> Arc<T> HashMap<K, V> //impl drop trait /** deref pointer 自动解引用 */ impl<T: ?Sized> Deref for &mut T { type Target = T; fn deref(&self) -> &T { *self } } struct User { name: &'static str }
    展开
    
    
  • jm26
    2020-10-16
    您好,您的编程之道在讲trait对象时关于trait不能是Sized的,原因是实现该trait的类可能没有实现Sized,但是既然是trait是继承的,为什么会出现实现了该trait却没有实现继承的Sized呢?
    共 5 条评论
    