rust-by-practice/solutions/compound-types/tuple.md

72 lines
1.0 KiB
Markdown
Raw Permalink Normal View History

2022-03-02 07:16:46 -06:00
1.
2022-05-01 00:08:50 -05:00
2022-03-02 07:16:46 -06:00
```rust
fn main() {
let _t0: (u8,i16) = (0, -1);
// Tuples can be tuple's members
let _t1: (u8, (i16, u32)) = (0, (-1, 1));
let t: (u8, u16, i64, &str, String) = (1u8, 2u16, 3i64, "hello", String::from(", world"));
}
```
2.
2022-05-01 00:08:50 -05:00
2022-03-02 07:16:46 -06:00
```rust
fn main() {
let t = ("i", "am", "sunface");
assert_eq!(t.2, "sunface");
}
```
3.
2022-05-01 00:08:50 -05:00
2022-03-02 07:16:46 -06:00
```rust
fn main() {
let too_long_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
println!("too long tuple: {:?}", too_long_tuple);
}
```
4.
2022-05-01 00:08:50 -05:00
2022-03-02 07:16:46 -06:00
```rust
fn main() {
let tup = (1, 6.4, "hello");
let (x, z, y) = tup;
assert_eq!(x, 1);
assert_eq!(y, "hello");
assert_eq!(z, 6.4);
}
```
5.
2022-05-01 00:08:50 -05:00
2022-03-02 07:16:46 -06:00
```rust
fn main() {
let (x, y, z);
// fill the blank
(y, z, x) = (1, 2, 3);
assert_eq!(x, 3);
assert_eq!(y, 1);
assert_eq!(z, 2);
}
```
6.
2022-05-01 00:08:50 -05:00
2022-03-02 07:16:46 -06:00
```rust
fn main() {
let (x, y) = sum_multiply((2, 3));
assert_eq!(x, 5);
assert_eq!(y, 6);
}
fn sum_multiply(nums: (i32, i32)) -> (i32, i32) {
(nums.0 + nums.1, nums.0 * nums.1)
}
```