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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/// # Vec 动态数组 /// /// Base usage: Vec基本用法 /// /// ```rust /// fn main() { /// let mut vec = Vec::new(); /// vec.push(1); /// vec.push(2); /// assert_eq!(vec.len(), 2); /// assert_eq!(vec[0], 1); /// assert_eq!(vec.pop(), Some(2)); /// assert_eq!(vec.len(), 1); /// vec[0] = 7; /// assert_eq!(vec[0], 7); /// assert_eq!(vec.get(0), Some(&7)); /// assert_eq!(vec.get(10), None); /// // vec[10]; /// vec.extend([1, 2, 3].iter().cloned()); /// assert_eq!(vec, [7, 1, 2, 3]); /// assert_eq!(vec.get(0..2), Some(&[7,1][..])); /// let mut vec2 = vec![4, 5, 6]; /// vec.append(&mut vec2); /// assert_eq!(vec, [7, 1, 2, 3, 4, 5, 6]); /// assert_eq!(vec2, []); /// vec.swap(1, 3); /// assert!(vec == [7, 3, 2, 1, 4, 5, 6]); /// let slice = [1, 2, 3, 4, 5, 6, 7]; /// vec.copy_from_slice(&slice); /// assert_eq!(vec, slice); /// let slice = [4, 3, 2, 1]; /// vec.clone_from_slice(&slice); /// assert_eq!(vec, slice); /// } /// ``` /// /// Base usage: 使用with_capacity预分配容量 /// /// ```rust /// fn main() { /// let mut vec = Vec::with_capacity(10); /// for i in 0..10 {vec.push(i);} /// vec.truncate(0); /// assert_eq!(10, vec.capacity()); /// for i in 0..10 { vec.push(i);} /// vec.clear(); /// assert_eq!(10, vec.capacity()); /// vec.shrink_to_fit(); /// assert_eq!(0, vec.capacity()); /// for i in 0..10 { /// vec.push(i); /// // output: 4/4/4/4/8/8/8/8/16/16/ /// print!("{:?}/", vec.capacity()); /// } /// } /// ``` /// /// Base usage: Vec中存储ZST类型 /// /// ```rust /// struct Foo; /// fn main(){ /// let mut vec = Vec::new(); /// vec.push(Foo); /// assert_eq!(vec.capacity(), std::usize::MAX); /// } /// ``` /// /// Base usage: Vec查找 /// /// ```rust /// fn main() { /// let v = [10, 40, 30]; /// assert!(v.contains(&30)); /// assert!(!v.contains(&50)); /// assert!(v.starts_with(&[10])); /// assert!(v.starts_with(&[10, 40])); /// assert!(v.ends_with(&[30])); /// assert!(v.ends_with(&[40, 30])); /// assert!(v.ends_with(&[])); /// let v: &[u8] = &[]; /// assert!(v.starts_with(&[])); /// assert!(v.ends_with(&[])); /// } /// ``` /// /// Base usage: Vec二分查找 /// /// ```rust /// fn main() { /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; /// assert_eq!(s.binary_search(&13), Ok(9)); /// assert_eq!(s.binary_search(&4), Err(7)); /// let r = s.binary_search(&1); /// assert!(match r { Ok(1...4) => true, _ => false, }); /// let seek = 13; /// assert_eq!( /// s.binary_search_by(|probe| probe.cmp(&seek)), /// Ok(9) /// ); /// let s = [(0, 0), (2, 1), (4, 1), (5, 1), (3, 1), /// (1, 2), (2, 3), (4, 5), (5, 8), (3, 13), /// (1, 21), (2, 34), (4, 55)]; /// assert_eq!( /// s.binary_search_by_key(&13, |&(a,b)| b), /// Ok(9) /// ); /// } /// ``` /// /// Base usage: Vec排序sort系列方法 /// /// ```rust /// fn main() { /// let mut v = [-5i32, 4, 1, -3, 2]; /// v.sort(); /// assert!(v == [-5, -3, 1, 2, 4]); /// v.sort_by(|a, b| a.cmp(b)); /// assert!(v == [-5, -3, 1, 2, 4]); /// v.sort_by(|a, b| b.cmp(a)); /// assert!(v == [4, 2, 1, -3, -5]); /// v.sort_by_key(|k| k.abs()); /// assert!(v == [1, 2, -3, 4, -5]); /// } /// ``` /// /// Base usage: 比较操作示意 /// /// ```rust /// use std::cmp::Ordering; /// fn main(){ /// let result = 1.0.partial_cmp(&2.0); /// assert_eq!(result, Some(Ordering::Less)); /// let result = 1.cmp(&1); /// assert_eq!(result, Ordering::Equal); /// let result = "abc".partial_cmp(&"Abc"); /// assert_eq!(result, Some(Ordering::Greater)); /// let mut v: [f32; 5] = [5.0, 4.1, 1.2, 3.4, 2.5]; /// v.sort_by(|a, b| a.partial_cmp(b).unwrap()); /// assert!(v == [1.2, 2.5, 3.4, 4.1, 5.0]); /// v.sort_by(|a, b| b.partial_cmp(a).unwrap()); /// assert!(v == [5.0, 4.1, 3.4, 2.5, 1.2]); /// } /// ``` /// /// Base usage: Rust 2018 edition 新语法,匹配数组 /// /// 基于此语法可以实现变长参数函数 /// ```rust /// fn pick(arr: [i32; 3]) { /// match arr { /// [_, _, 3] => println!("ends with 3"), /// [a, 2, c] => println!("{:?}, 2, {:?}", a, c), /// [_, _, _] => println!("pass!"), /// } /// } /// fn main(){ /// let arr = [1, 2, 3]; /// pick(arr); /// let arr = [1, 2, 5]; /// pick(arr); /// let arr = [1, 3, 5]; /// pick(arr); /// } /// ``` /// /// Base usage: Rust 2018 edition 新语法,匹配切片 /// /// 基于此语法可以实现变长参数函数 /// ```rust /// fn sum(num: &[i32]) { /// match num { /// [one] => println!(" at least two"), /// [first, second] => println!("{:?} + {:?} = {:?} ", first, second, first+second), /// _ => println!("sum is {:?}", num.iter().fold(0, |sum, i| sum + i) ), /// } /// } /// fn main() { /// sum(&[1]); /// sum(&[1, 2]); /// sum(&[1, 2, 3]); /// sum(&[1, 2, 3, 5]); /// } /// ``` pub fn bases(){ unimplemented!(); }