1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/// # 类型转换:自动解引用(For SmartPointer) /// /// Base usage: `Vec<T>`实现了`Deref<Target=[T]>` /// /// ``` /// fn foo(s: &[i32]){ /// println!("{:?}", s[0]); /// } /// /// let v = vec![1,2,3]; /// foo(&v) /// ``` /// /// Base usage: `String`实现了`Deref<Target=str>` /// /// ``` /// let a = "hello".to_string(); /// let b = " world".to_string(); /// let c = a + &b; /// println!("{:?}", c); // "hello world" /// ``` // /// Base usage: `Rc<T>`实现了`Deref<Target<T>>` /// /// ``` /// use std::rc::Rc; /// let x = Rc::new("hello"); /// println!("{:?}", x.chars()); /// ``` pub fn auto_deref(){ fn foo(s: &[i32]){ println!("{:?}", s[0]); } let v = vec![1,2,3]; foo(&v) } /// # 类型转换:手动解引用(For SmartPointer) /// /// Base usage: `Rc` 和 `&str` 都实现了clone /// /// ``` /// use std::rc::Rc; /// /// let x = Rc::new("hello"); /// let y = x.clone(); // Rc<&str> /// let z = (*x).clone(); // &str /// ``` /// /// Base usage: match匹配里需要手动解引用 /// /// 将match &x改为以下5种形式任意一种即可: /// - match x.deref(),直接调用deref方法,需要use std::ops::Deref。 /// - match x.as_ref(),String类型提供了as_ref方法来返回一个&str类型,该方法定义于AsRef trait中。 /// - match x.borrow(),方法borrow定义于Borrow trait中,行为和AsRef类型。需要use std::borrow::Borrow。 /// - match &*x,使用“解引用”操作符,将String转换为str,然后再用“引用”操作符转为&str。 /// - match &x[..],这是因为String类型的index操作可以返回&str类型。 /// /// ``` /// let x = "hello".to_string(); /// match &x { /// "hello" => {println!("hello")}, /// _ => {} /// } /// ``` pub fn manual_deref(){ use std::rc::Rc; let x = Rc::new("hello"); let y = x.clone(); // Rc<&str> let z = (*x).clone(); // &str } /// # 类型转换:as 操作符 /// /// Base usage: 无歧义完全限定语法(Fully Qualified Syntax for Disambiguation) /// 曾用名: 通用函数调用语法(UFCS) /// ``` /// struct S(i32); /// trait A { /// fn test(&self, i: i32); /// } /// trait B { /// fn test(&self, i: i32); /// } /// impl A for S { /// fn test(&self, i: i32) { /// println!("From A: {:?}", i); /// } /// } /// impl B for S { /// fn test(&self, i: i32) { /// println!("From B: {:?}", i+1); /// } /// } /// /// let s = S(1); /// A::test(&s, 1); /// B::test(&s, 1); /// <S as A>::test(&s, 1); /// <S as B>::test(&s, 1); /// ``` /// /// Base usage: 父类型子类型相互转换 /// ``` /// let a: &'static str = "hello"; // &'static str /// let b: &str = a as &str; // &str /// let c: &'static str = b as &'static str; // &'static str /// ``` pub fn fqsfd(){ struct S(i32); trait A { fn test(&self, i: i32); } trait B { fn test(&self, i: i32); } impl A for S { fn test(&self, i: i32) { println!("From A: {:?}", i); } } impl B for S { fn test(&self, i: i32) { println!("From B: {:?}", i+1); } } let s = S(1); A::test(&s, 1); B::test(&s, 1); <S as A>::test(&s, 1); <S as B>::test(&s, 1); } /// # 类型转换:From和Into /// /// Base usage: String实现了From /// /// ``` /// let string = "hello".to_string(); /// let other_string = String::from("hello"); /// assert_eq!(string, other_string); /// ``` /// /// Base usage: 使用into简化代码 /// /// ``` /// #[derive(Debug)] /// struct Person{ name: String } /// impl Person { /// fn new<T: Into<String>>(name: T) -> Person { /// Person {name: name.into()} /// } /// } /// let person = Person::new("Alex"); /// let person = Person::new("Alex".to_string()); /// println!("{:?}", person); /// ``` pub fn from_into(){ let string = "hello".to_string(); let other_string = String::from("hello"); assert_eq!(string, other_string); }