Skip to content

元组

129字小于1分钟

2022-06-03

元组是由多种类型组合到一起形成的,因此它是复合类型,元组的长度是固定的,元组中元素的顺序也是固定的。

fn main() {
	let tup: (i32, u32, String) = (10, 20, String::from("hello"));
	println!("{:?}", tup);
}

模式匹配解构元组

fn main() {
	let tup: (i32, u32, String) = (10, 20, String::from("hello"));
	let (x, y, s) = tup;
	println!("{}, {}, {}", x, y, s);
}

. 访问元组

fn main() {
	let tup: (i32, u32, String) = (10, 20, String::from("hello"));
	println!("{}, {}, {}", tup.0, tup.1, tup.2);
}