- 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.
推薦閱讀
- ASP.NET Core 5.0開發(fā)入門與實戰(zhàn)
- C語言程序設計教程(第2版)
- Web全棧工程師的自我修養(yǎng)
- 常用工具軟件立體化教程(微課版)
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook
- Oracle GoldenGate 12c Implementer's Guide
- Laravel Application Development Blueprints
- Extending Unity with Editor Scripting
- Microsoft HoloLens By Example
- Python數(shù)據(jù)科學實踐指南
- 產(chǎn)品架構評估原理與方法
- Django 3 Web Development Cookbook
- Scratch超人漫游記:創(chuàng)意程序設計:STEAM創(chuàng)新教育指南
- Python編程:從入門到實踐(第2版)
- R High Performance Programming