diff --git a/solutions/compound-types/tuple.md b/solutions/compound-types/tuple.md index e69de29..d00125d 100644 --- a/solutions/compound-types/tuple.md +++ b/solutions/compound-types/tuple.md @@ -0,0 +1,66 @@ +1. +```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. +```rust +fn main() { + let t = ("i", "am", "sunface"); + assert_eq!(t.2, "sunface"); + } +``` + +3. +```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. +```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. +```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. +```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) + } +``` \ No newline at end of file diff --git a/src/compound-types/tuple.md b/src/compound-types/tuple.md index 55f1d3a..df793a5 100644 --- a/src/compound-types/tuple.md +++ b/src/compound-types/tuple.md @@ -1,5 +1,5 @@ # Tuple -🌟 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. +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. ```rust,editable fn main() { @@ -11,7 +11,7 @@ fn main() { } ``` -🌟 Members can be extracted from the tuple using indexing. +2. 🌟 Members can be extracted from the tuple using indexing. ```rust,editable // make it works @@ -21,7 +21,7 @@ fn main() { } ``` -🌟 Long tuples cannot be printed +3. 🌟 Long tuples cannot be printed ```rust,editable // fix the error @@ -31,7 +31,7 @@ fn main() { } ``` -🌟 Destructuring tuple with pattern. +4. 🌟 Destructuring tuple with pattern. ```rust,editable fn main() { @@ -46,7 +46,7 @@ fn main() { } ``` -🌟🌟 Destructure assignments. +5. 🌟🌟 Destructure assignments. ```rust,editable fn main() { let (x, y, z); @@ -60,7 +60,7 @@ fn main() { } ``` -🌟🌟 Tuples can be used as function arguments and return values +6. 🌟🌟 Tuples can be used as function arguments and return values ```rust,editable fn main() {