【Rust练习】10.元组
练习题来自:https://practice-zh.course.rs/compound-types/tuple.html
1 元组中的元素可以是不同的类型。元组的类型签名是 (T1, T2, …), 这里 T1, T2 是相对应的元组成员的类型.
fn main() {
let _t0: (u8,i16) = (0, -1);
// 元组的成员还可以是一个元组
let _t1: (u8, (i16, u32)) = (0, (-1, 1));
// 填空让代码工作
let t: (u8, __, i64, __, __) = (1u8, 2u16, 3i64, "hello", String::from(", world"));
}
我在这里说一下我对元组的理解,这个东西类似于C
的union
,也就是C++
的variant
,这两个类型我都有文章讲过:
【C++】union
【C++】std::variant
至于这道题,很简答,写出对应的类型就行:
fn main() {
let _t0: (u8,i16) = (0, -1);
// 元组的成员还可以是一个元组
let _t1: (u8, (i16, u32)) = (0, (-1, 1));
// 填空让代码工作
let t: (u8, u16, i64, &str, String) = (1u8, 2u16, 3i64, "hello", String::from(", world"));
}
2 🌟 可以使用索引来获取元组的成员
// 修改合适的地方,让代码工作
fn main() {
let t = ("i", "am", "sunface");
assert_eq!(t.1, "sunface");
}
surface是第三个,索引是2
fn main() {
let t = ("i", "am", "sunface");
assert_eq!(t.2, "sunface");
}
3 🌟 过长的元组无法被打印输出
// 修复代码错误
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);
}
目前仅仅为大小不超过 12 的元组实现了 Debug,更长的元组未实现(而且可能永远无法实现了)。
fn main() {
let too_long_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
let sup: (i32, i32) = (12, 13);
println!("too long tuple: {:?} {:?}", too_long_tuple, sup);
}
4 使用模式匹配来解构元组
fn main() {
let tup = (1, 6.4, "hello");
// 填空
let __ = tup;
assert_eq!(x, 1);
assert_eq!(y, "hello");
assert_eq!(z, 6.4);
}
其实就是给对应位置的参数起个别名。
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 🌟🌟 解构式赋值
fn main() {
let (x, y, z);
// 填空
__ = (1, 2, 3);
assert_eq!(x, 3);
assert_eq!(y, 1);
assert_eq!(z, 2);
}
同上
fn main() {
let (x, y, z);
// 填空
(y, z, x) = (1, 2, 3);
assert_eq!(x, 3);
assert_eq!(y, 1);
assert_eq!(z, 2);
}
6 🌟🌟 元组可以用于函数的参数和返回值
fn main() {
// 填空,需要稍微计算下
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)
}
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)
}