fnmain() { let tup = (500, 6.4, 1); let (_x, y, _z) = tup; println!("The value of y is: {}", y); }
然后重新编译一下:
1 2 3 4
Compiling learn_rust_by_example v0.1.0 (F:\workspace\rust\learn_rust_by_example) Finished dev [unoptimized + debuginfo] target(s) in 0.90s Running `target\debug\learn_rust_by_example.exe` The value of y is: 6.4
println!("The value of y is: {}, x is {}", y,_x); }
结果如下:
1 2 3 4
Compiling learn_rust v0.1.0 (/Users/leetao/Workspace/rt/learn_rust) Finished dev [unoptimized + debuginfo] target(s) in2.86s Running `target/debug/learn_rust` The value of y is: 6.4, x is 500
eg:_
1 2 3 4 5
fnmain() { let tup = (500, 6.4, 1); let (_x, _y, _) = tup; println!("The value of _ is {}", _); }
结果如下:
1 2 3 4 5 6 7 8
Compiling learn_rust v0.1.0 (/Users/leetao/Workspace/rt/learn_rust) error: expected expression, found reserved identifier `_` --> src/main.rs:4:39 | 4 | println!("The value of _ is {}", _); | ^ expected expression
fnmain() { let x: (i32, f64, u8) = (500, 6.4, 1); let first = x.0; // 索引值也是从 0 开始 let second = x.1; let third = x.2; println!("first :{}, second:{}, third: {}",first,second,third); }
看一下编辑结果:
1 2 3 4
Compiling learn_rust v0.1.0 (/Users/leetao/Workspace/rt/learn_rust) Finished dev [unoptimized + debuginfo] target(s) in0.46s Running `target/debug/learn_rust` first :500, second:6.4, third: 1
fnmain() { let a = [1, 2, 3, 4, 5]; // 写法一 // let a: [i32; 5] = [1, 2, 3, 4, 5]; 写法二 let first = a[0]; let second = a[1]; println!("first:{}, second:{}",first,second); }
fnmain() { let a = [1, 2, 3, 4, 5]; let index = 10; let element = a[index]; println!("The value of element is: {}", element); }
看一下结果:
1 2 3 4 5
Compiling learn_rust v0.1.0 (/Users/leetao/Workspace/rt/learn_rust) Finished dev [unoptimized + debuginfo] target(s) in0.07s Running `target/debug/learn_rust` thread 'main' panicked at 'index out of bounds: the len is 5 but the index is 10', src/main.rs:4:19 note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.