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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/// # 基本数据类型:布尔值
///
/// Basic usage:
///
/// ```
/// fn bool_type() {
///     let x = true;  // bool类型可以自动推断
///     let y: bool = false;  // bool值分为true和false
///     let x = 5;
///     if x > 1 { println!( "x is bigger than 1")};  // 比较操作会产生一个bool类型
///     assert_eq!(x as i32, 1);
///     assert_eq!(y as i32, 0);  // 通过as操作符可以将bool转换为数字
/// }
/// bool_type();
/// ```
pub fn bool_type() {
    let x = true;
    let y: bool = false;
    let x = 5;
    if x > 1 { println!( "x is bigger than 1")};
    assert_eq!(x as i32, 1);
    assert_eq!(y as i32, 0);
}

/// # 基本数据类型:数字类型
///
/// Basic usage:
///
/// ```
/// fn number_type() {
///     let num = 42u32;
///     let num: u32 = 42;
///     let num = 0x2A;  // 16进制
///     let num = 0o106;  // 8进制
///     let num = 0b1101_1011; // 2进制
///     assert_eq!(b'*', 42u8); // 字节字面量
///     assert_eq!(b'\'', 39u8);
///     let num = 3.1415926f64;
///     assert_eq!(-3.14, -3.14f64);
///     assert_eq!(2., 2.0f64);
///     assert_eq!(2e4, 20000f64);
///     println!("{:?}", std::f32::INFINITY);
///     println!("{:?}", std::f32::NEG_INFINITY);
///     println!("{:?}", std::f32::NAN);
///     println!("{:?}", std::f32::MIN);
///     println!("{:?}", std::f32::MAX);
/// }
/// number_type();
/// ```
pub fn number_type() {
    use std::f32::{INFINITY, NEG_INFINITY, NAN, MIN, MAX};
    let num = 42u32;
    let num: u32 = 42;
    let num = 0x2A;  // 16进制
    let num = 0o106;  // 8进制
    let num = 0b1101_1011; // 2进制
    assert_eq!(b'*', 42u8); // 字节字面量
    assert_eq!(b'\'', 39u8);
    let num = 3.1415926f64;
    assert_eq!(-3.14, -3.14f64);
    assert_eq!(2., 2.0f64);
    assert_eq!(2e4, 20000f64);
    println!("{:?}", INFINITY);
    println!("{:?}", NEG_INFINITY);
    println!("{:?}", NAN);
    println!("{:?}", MIN);
    println!("{:?}", MAX);
}


/// # 基本数据类型:char类型
///
/// Basic usage:
///
/// ```
/// fn char_type(){
///     let x = 'r';
///     let x = 'Ú';
///     println!("{}", '\'');
///     println!("{}", '\\');
///     println!("{}", '\n');
///     println!("{}", '\r');
///     println!("{}", '\t');
///     assert_eq!('\x2A', '*');
///     assert_eq!('\x25', '%');
///     assert_eq!('\u{CA0}', 'ಠ');
///     assert_eq!('\u{151}', 'ő');
///     assert_eq!('%' as i8, 37);
///     assert_eq!('ಠ' as i8, -96); //该字符值的高位会被截断,最终得到-96
/// }
/// char_type();
/// ```
pub fn char_type(){
    let x = 'r';
    let x = 'Ú';
    println!("{}", '\'');
    println!("{}", '\\');
    println!("{}", '\n');
    println!("{}", '\r');
    println!("{}", '\t');
    assert_eq!('\x2A', '*');
    assert_eq!('\x25', '%');
    assert_eq!('\u{CA0}', 'ಠ');
    assert_eq!('\u{151}', 'ő');
    assert_eq!('%' as i8, 37);
    assert_eq!('ಠ' as i8, -96); //该字符值的高位会被截断,最终得到-96
}

/// # 基本数据类型:array类型
///
/// Basic usage:
///
/// ```
/// fn array_type() {
///     let arr: [i32; 3] = [1, 2, 3];       // 定义一个[i32; 3]类型的数组,默认不可变
///     let mut mut_arr = [1, 2, 3];        // 定义一个可变数组
///     assert_eq!(1, mut_arr[0]);         // 数组索引从0开始,验证第一位元素等于1
///     mut_arr[0] = 3;                            // 修改mut_arr第一个元素为3,因为它是可变数组
///     assert_eq!(3, mut_arr[0]);        // 验证修改之后的mut_arr数组第一个元素为3
///     let init_arr = [0; 10];                  // 创建一个初始值为0,长度为10的数组
///     assert_eq!(0, init_arr[5]);         // 通过数组下标访问数组元素,验证init_arr数组中任意一个元素的值是否为0
///     assert_eq!(10, init_arr.len());  // 验证数组的长度是否为10
///     // println!("{:?}", arr[5]); // error: 索引超出范围
/// }
/// array_type();
/// ```
pub fn array_type() {
    let arr: [i32; 3] = [1, 2, 3];
    let mut mut_arr = [1, 2, 3];
    assert_eq!(1, mut_arr[0]);
    mut_arr[0] = 3;
    assert_eq!(3, mut_arr[0]);
    let init_arr = [0; 10];
    assert_eq!(0, init_arr[5]);
    assert_eq!(10, init_arr.len());
    // println!("{:?}", arr[5]); //error
}

/// # 基本数据类型:range类型
///
/// Basic usage:
///
/// ```
/// fn range_type(){
///     // (1..5)是结构体std::ops::Range的一个实例
///     assert_eq!((1..5), std::ops::Range{ start: 1, end: 5 });
///     // (1..=5)是结构体std::ops::Range的一个实例
///     assert_eq!((1..=5), std::ops::RangeInclusive::new(1, 5));
///     assert_eq!(3+4+5, (3..6).sum());
///     assert_eq!(3+4+5+6, (3..=6).sum());
///     // 每个范围都是一个迭代器,可用for 循环打印范围内的元素
///     for i in (1..5) {
///         println!("{}", i);
///     }
///     for i in (1..=5) {
///         println!("{}", i);
///     }
/// }
/// range_type();
/// ```
pub fn range_type(){
    // (3..5)是结构体std::ops::Range的一个实例
    use std::ops::{Range, RangeInclusive};
    assert_eq!((1..5), Range{ start: 1, end: 5 });
    assert_eq!((1..=5), RangeInclusive::new(1, 5));
    assert_eq!(3+4+5, (3..6).sum());
    assert_eq!(3+4+5+6, (3..=6).sum());

    // 每个范围都是一个迭代器,可用for 循环打印范围内的元素
    for i in (1..5) {
        println!("{}", i);
    }
    for i in (1..=5) {
        println!("{}", i);
    }
}

/// # 基本数据类型:slice类型
///
/// Basic usage:
///
/// ```
/// fn slice_type() {
///     let arr: [i32; 5] = [1, 2, 3, 4, 5];
///     assert_eq!(&arr, &[1, 2,3,4,5]);
///     assert_eq!(&arr[1..], [2,3,4,5]);
///     assert_eq!(&arr.len(), &5);
///     assert_eq!(&arr.is_empty(), &false);
///     let arr = &mut [1, 2, 3];
///     arr[1] = 7;
///     assert_eq!(arr, &[1, 7, 3]);
///     let vec = vec![1, 2, 3];
///     assert_eq!(&vec[..], [1,2,3]);
///     let str_slice: &[&str] = &["one", "two", "three"];
///     assert_eq!(str_slice, ["one", "two", "three"]);
/// }
/// slice_type();
/// ```
pub fn slice_type() {
    let arr: [i32; 5] = [1, 2, 3, 4, 5];
    assert_eq!(&arr, &[1, 2,3,4,5]);
    assert_eq!(&arr[1..], [2,3,4,5]);
    assert_eq!(&arr.len(), &5);
    assert_eq!(&arr.is_empty(), &false);
    let arr = &mut [1, 2, 3];
    arr[1] = 7;
    assert_eq!(arr, &[1, 7, 3]);
    let vec = vec![1, 2, 3];
    assert_eq!(&vec[..], [1,2,3]);
    let str_slice: &[&str] = &["one", "two", "three"];
    assert_eq!(str_slice, ["one", "two", "three"]);
}

/// # 基本数据类型:str字符串类型
///
/// Basic usage:
///
/// ```
/// fn str_type(){
///     let truth = "Rust是一门优雅的语言";
///     let ptr = truth.as_ptr();
///     let len = truth.len();
///     assert_eq!(28, len);
///     let s = unsafe {
///         let slice = std::slice::from_raw_parts(ptr, len);
///         std::str::from_utf8(slice)
///     };
///     assert_eq!(s, Ok(truth));
/// }
/// str_type();
/// ```
pub fn str_type(){
    use std::slice::from_raw_parts;
    use std::str::from_utf8;
    let truth: &'static str = "Rust是一门优雅的语言";
    let ptr = truth.as_ptr();
    let len = truth.len();
    assert_eq!(28, len);
    let s = unsafe {
        let slice = from_raw_parts(ptr, len);
        from_utf8(slice)
    };
    assert_eq!(s, Ok(truth));
}

fn move_coords( x: (i32,i32) ) -> (i32, i32) {
    (x.0 + 1, x.1 + 1)
}
/// # 基本数据类型:元组类型
///
/// Basic usage:
///
/// ```
/// // 利用元组返回多个值
/// fn move_coords( x: (i32,i32) ) -> (i32, i32) {
///     (x.0 + 1, x.1 + 1)
/// }
///
/// fn tuple_type(){
///     let tuple : (&'static str, i32, char) = ("hello", 5, 'c');  // 长度为3的异构序列
///     assert_eq!(tuple.0, "hello");  // 通过索引获取元组元素
///     assert_eq!(tuple.1, 5);
///     assert_eq!(tuple.2, 'c');
///     let coords = (0, 1);
///     let result = move_coords(coords);
///     assert_eq!(result, (1, 2));  //  move_coords函数返回元组
///     let (x, y) =  move_coords(coords); //  let 模式匹配解构操作
///     assert_eq!(x, 1);
///     assert_eq!(y, 2);
/// }
/// tuple_type();
/// ```
pub fn tuple_type(){
    let tuple = ("hello", 5, 'c');
    assert_eq!(tuple.0, "hello");
    assert_eq!(tuple.1, 5);
    assert_eq!(tuple.2, 'c');
    let coords = (0, 1);
    let result = move_coords(coords);
    assert_eq!(result, (1, 2));
    let (x, y) =  move_coords(coords);
    assert_eq!(x, 1);
    assert_eq!(y, 2);
}

/// # 基本数据类型:原始指针类型
///
/// Basic usage:
///
/// ```
/// // 利用元组返回多个值
/// fn raw_pointer() {
///     let mut x = 10;
///     let ptr_x = &mut x as *mut i32; // 通过as转换为*mut T原始指针
///     let y = Box::new(20);  // 使用Box语法代表在堆内存中存放数字20
///     let ptr_y = &*y as *const i32; // 通过as转换为*const T原始指针
///     unsafe {
///     // 使用它们必须置于unsafe块中
///     // *ptr_x 是可变的,*ptr_y是只读的
///     // 所以这里不会有安全问题
///     *ptr_x += *ptr_y;
///     }
///     assert_eq!(x, 30);
/// }
/// raw_pointer();
/// ```
pub fn raw_pointer() {
    let mut x = 10;
    let ptr_x = &mut x as *mut i32;
    let y = Box::new(20);
    let ptr_y = &*y as *const i32;
    unsafe {
      *ptr_x += *ptr_y;
    }
    assert_eq!(x, 30);
}

/// # 基本数据类型:nerver类型
///
/// Basic usage:
///
/// ```
/// #![feature(never_type)]
///
/// fn foo() -> u32 {
///     let x: ! = {
///         return 123
///     };
/// }
///
/// fn nerver(){
///     let num: Option<u32> = Some(42);
///     match num {
///         Some(num) => num,
///         None => panic!("Nothing!"),
///     };
/// }
/// foo();
/// nerver();
/// ```
pub fn nerver(){
    let num: Option<u32> = Some(42);
    match num {
        Some(num) => num,
        None => panic!("Nothing!"),
    };
}

fn foo() -> u32 {
    let x: ! = {
        return 123
    };
}