- Rust Programming By Example
- Guillaume Gomez Antoni Boucher
- 119字
- 2021-07-02 19:12:58
Tuples
Tuples and structures are similar, except that tuples' fields are unnamed. Tuples are declared inside parentheses, with the element separated by a comma:
let tuple = (24, 42); println!("({}, {})", tuple.0, tuple.1);
As you can see on the second line, we can access the elements of a tuple with .index, where index is a constant and this index starts at 0.
Tuples can be used to return multiple values from a function. For instance, the str::split_at() method returns two strings:
let (hello, world) = "helloworld".split_at(5); println!("{}, {}!", hello, world);
Here, we assign the two elements of the tuple to the hello and world variables. We'll see why this works in the Pattern matching section.
推薦閱讀
- 情報學與信息管理的主戰(zhàn)場:北京大學情報學與信息管理論壇文集
- 科學普及組織活動讀本
- 公共文化館治理研究(2015年):四川省文化館“十三五”規(guī)劃重大課題調(diào)研成果集
- 非物質(zhì)文化遺產(chǎn)數(shù)字化研究
- 高校圖書館閱讀推廣與宣傳促進研究
- 清代地方檔案的保存、整理與研究
- 信息系統(tǒng)工程(第2版)
- 檔案利用與服務
- 圖書情報專業(yè)碩士培養(yǎng)研究與實踐
- 高校圖書館工具書管理與服務研究
- 檔案與少數(shù)民族記憶
- 大數(shù)據(jù)時代科研人員個人學術(shù)信息管理行為
- 兒童閱讀的世界Ⅳ:學校、家庭與社區(qū)的實踐研究
- 圖書館服務均等化與資源共享(下冊)
- 檔案學視野下的東巴古籍文獻遺產(chǎn)保護研究