- Mastering Rust
- Rahul Sharma Vesa Kaihlavirta
- 149字
- 2021-07-02 13:35:17
Tuples
Tuples differ from arrays in the way that elements of an array have to be of the same type, while items in a tuple can be a mix of types. They are heterogeneous collections and are useful for storing distinct types together. They can also be used when returning multiple values from a function. Consider the following code that uses tuples:
// tuples.rs
fn main() {
let num_and_str: (u8, &str) = (40, "Have a good day!");
println!("{:?}", num_and_str);
let (num, string) = num_and_str;
println!("From tuple: Number: {}, String: {}", num, string);
}
In the preceding code, num_and_str is a tuple of two items, (u8, &str). We can also extract values from an already declared tuple into individual variables. After printing the tuple, we destructure it on the next line into the num and string variables, and their types are inferred automatically. That's pretty neat.
推薦閱讀
- Clojure Programming Cookbook
- Debian 7:System Administration Best Practices
- Programming ArcGIS 10.1 with Python Cookbook
- 數(shù)據(jù)結(jié)構(gòu)(C語(yǔ)言)
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- 飛槳PaddlePaddle深度學(xué)習(xí)實(shí)戰(zhàn)
- RSpec Essentials
- 微服務(wù)從小白到專家:Spring Cloud和Kubernetes實(shí)戰(zhàn)
- 匯編語(yǔ)言編程基礎(chǔ):基于LoongArch
- 一步一步跟我學(xué)Scratch3.0案例
- C++程序設(shè)計(jì)教程(第2版)
- Offer來了:Java面試核心知識(shí)點(diǎn)精講(框架篇)
- Unity 3D UI Essentials
- Learning Perforce SCM
- Mastering Vim