2022-02-27 08:10:59 -06:00
|
|
|
# Tuple
|
2022-03-02 07:16:46 -06:00
|
|
|
1. π Elements in a tuple can have different types. Tuple's type signature is `(T1, T2, ...)`, where `T1`, `T2` are the types of tuple's members.
|
2022-02-27 08:10:59 -06:00
|
|
|
```rust,editable
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let _t0: (u8,i16) = (0, -1);
|
|
|
|
// Tuples can be tuple's members
|
|
|
|
let _t1: (u8, (i16, u32)) = (0, (-1, 1));
|
|
|
|
// fill the blanks to make the code work
|
|
|
|
let t: (u8, __, i64, __, __) = (1u8, 2u16, 3i64, "hello", String::from(", world"));
|
2022-03-05 00:48:43 -06:00
|
|
|
|
|
|
|
println!("Success!")
|
2022-02-27 08:10:59 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-03-02 07:16:46 -06:00
|
|
|
2. π Members can be extracted from the tuple using indexing.
|
2022-02-27 08:10:59 -06:00
|
|
|
```rust,editable
|
|
|
|
|
|
|
|
// make it works
|
|
|
|
fn main() {
|
2022-03-25 01:43:12 -06:00
|
|
|
let t = ("i", "am", "sunface");
|
|
|
|
assert_eq!(t.1, "sunface");
|
2022-03-05 00:48:43 -06:00
|
|
|
|
2022-03-25 01:43:12 -06:00
|
|
|
println!("Success!")
|
2022-02-27 08:10:59 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-03-02 07:16:46 -06:00
|
|
|
3. π Long tuples cannot be printed
|
2022-02-27 08:10:59 -06:00
|
|
|
```rust,editable
|
|
|
|
|
|
|
|
// fix the error
|
|
|
|
fn main() {
|
|
|
|
let too_long_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
|
|
|
|
println!("too long tuple: {:?}", too_long_tuple);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-03-02 07:16:46 -06:00
|
|
|
4. π Destructuring tuple with pattern.
|
2022-02-27 08:10:59 -06:00
|
|
|
```rust,editable
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let tup = (1, 6.4, "hello");
|
|
|
|
|
|
|
|
// fill the blank to make the code work
|
|
|
|
let __ = tup;
|
|
|
|
|
|
|
|
assert_eq!(x, 1);
|
|
|
|
assert_eq!(y, "hello");
|
|
|
|
assert_eq!(z, 6.4);
|
2022-03-05 00:48:43 -06:00
|
|
|
|
|
|
|
println!("Success!")
|
2022-02-27 08:10:59 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-03-02 07:16:46 -06:00
|
|
|
5. ππ Destructure assignments.
|
2022-02-27 08:10:59 -06:00
|
|
|
```rust,editable
|
|
|
|
fn main() {
|
|
|
|
let (x, y, z);
|
|
|
|
|
2022-02-27 08:16:13 -06:00
|
|
|
// fill the blank
|
2022-02-27 08:10:59 -06:00
|
|
|
__ = (1, 2, 3);
|
|
|
|
|
|
|
|
assert_eq!(x, 3);
|
|
|
|
assert_eq!(y, 1);
|
|
|
|
assert_eq!(z, 2);
|
2022-03-05 00:48:43 -06:00
|
|
|
|
|
|
|
println!("Success!")
|
2022-02-27 08:10:59 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-03-02 07:16:46 -06:00
|
|
|
6. ππ Tuples can be used as function arguments and return values
|
2022-02-27 08:10:59 -06:00
|
|
|
```rust,editable
|
|
|
|
|
|
|
|
fn main() {
|
2022-03-25 01:43:12 -06:00
|
|
|
// fill the blank, need a few computations here.
|
|
|
|
let (x, y) = sum_multiply(__);
|
2022-02-27 08:10:59 -06:00
|
|
|
|
2022-03-25 01:43:12 -06:00
|
|
|
assert_eq!(x, 5);
|
|
|
|
assert_eq!(y, 6);
|
2022-03-05 00:48:43 -06:00
|
|
|
|
2022-03-25 01:43:12 -06:00
|
|
|
println!("Success!")
|
2022-02-27 08:10:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn sum_multiply(nums: (i32, i32)) -> (i32, i32) {
|
|
|
|
(nums.0 + nums.1, nums.0 * nums.1)
|
|
|
|
}
|
|
|
|
```
|
2022-03-01 08:06:38 -06:00
|
|
|
|
|
|
|
> You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it
|