From 18ddf5cbe86251a83af0b41494cd8df3d27e4586 Mon Sep 17 00:00:00 2001 From: sunface Date: Sun, 27 Feb 2022 22:10:59 +0800 Subject: [PATCH] add en/tuple.md --- en/src/SUMMARY.md | 2 +- en/src/compound-types/tuple.md | 77 +++++++++++++++++++++++++++++++++- zh-CN/src/SUMMARY.md | 2 +- 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/en/src/SUMMARY.md b/en/src/SUMMARY.md index c5e9708..1e1353b 100644 --- a/en/src/SUMMARY.md +++ b/en/src/SUMMARY.md @@ -14,7 +14,7 @@ - [string](compound-types/string.md) - [Array](compound-types/array.md) - [Slice](compound-types/slice.md) - - [Tuple todo](compound-types/tuple.md) + - [Tuple](compound-types/tuple.md) - [Struct todo](compound-types/struct.md) - [Enum todo](compound-types/enum.md) - [Flow Control todo](flow-control.md) diff --git a/en/src/compound-types/tuple.md b/en/src/compound-types/tuple.md index ebbc1a7..3339793 100644 --- a/en/src/compound-types/tuple.md +++ b/en/src/compound-types/tuple.md @@ -1 +1,76 @@ -# tuple +# 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. +```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")); +} +``` + +🌟 Members can be extracted from the tuple using indexing. +```rust,editable + +// make it works +fn main() { + let t = ("i", "am", "sunface"); + assert_eq!(t.1, "sunface"); +} +``` + +🌟 Long tuples cannot be printed +```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); +} +``` + +🌟 Destructuring tuple with patter. +```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); +} +``` + +🌟🌟 Destructure assignments. +```rust,editable +fn main() { + let (x, y, z); + + __ = (1, 2, 3); + + assert_eq!(x, 3); + assert_eq!(y, 1); + assert_eq!(z, 2); +} +``` + +🌟🌟 Tuples can be used as function arguments and return values +```rust,editable + +fn main() { + // fill the blank to make it work + let (x, y) = sum_multiply(__); + + assert_eq!(x, 5); + assert_eq!(y, 6); +} + +fn sum_multiply(nums: (i32, i32)) -> (i32, i32) { + (nums.0 + nums.1, nums.0 * nums.1) +} +``` diff --git a/zh-CN/src/SUMMARY.md b/zh-CN/src/SUMMARY.md index 60ef009..661359a 100644 --- a/zh-CN/src/SUMMARY.md +++ b/zh-CN/src/SUMMARY.md @@ -14,7 +14,7 @@ - [字符串](compound-types/string.md) - [ζ•°η»„](compound-types/array.md) - [εˆ‡η‰‡](compound-types/slice.md) - - [ε…ƒη»„ undo](compound-types/tuple.md) + - [ε…ƒη»„](compound-types/tuple.md) - [η»“ζž„δ½“ undo](compound-types/struct.md) - [枚举 undo](compound-types/enum.md) - [ζ΅η¨‹ζŽ§εˆΆ todo](flow-control.md)