- Rust Programming By Example
- Guillaume Gomez Antoni Boucher
- 168字
- 2021-07-02 19:12:58
Copy types
Some types are not moved when we assigned a value of these types to another variable. This is the case for basic types such as integers. For instance, the following code is perfectly valid:
let num1 = 42; let num2 = num1; println!("{}", num1);
We can still use num1 even thought we assigned it to num2. This is because the basic types implement a special marker: Copy. Copy types are copied instead of moved.
We can make our own types Copy by adding derive to them:
#[derive(Clone, Copy)] struct Point { x: i32, y: i32, }
Since Copy requires Clone, we also implement the latter for our Point type. We cannot derive Copy for a type containing a value that does not implement Copy. Now, we can use a Point without having to bother with references:
fn print_point(point: Point) { println!("x: {}, y: {}", point.x, point.y); } let p1 = Point { x: 1, y: 2 }; let p2 = p1; print_point(p1); println!("{}", p1.x);
推薦閱讀
- 普通高校中文學(xué)科基礎(chǔ)教材古典文獻(xiàn)學(xué)基礎(chǔ)
- 大數(shù)據(jù)環(huán)境下圖書館文獻(xiàn)信息資源建設(shè)與利用
- 區(qū)塊鏈在電子檔案管理中的應(yīng)用
- 智慧場(chǎng)館與智慧學(xué)習(xí)
- 企業(yè)數(shù)字檔案館建設(shè)理論與實(shí)踐
- Hands-On Concurrency with Rust
- 李一氓文存(第二卷):存在集三編下冊(cè)
- 知中16·西南聯(lián)大的遺產(chǎn)
- Rust Programming By Example
- 茶與咖啡:張信剛文化與經(jīng)濟(jì)講座
- 公共圖書館知識(shí)產(chǎn)權(quán)信息服務(wù)體系建設(shè)
- Web 2.0環(huán)境中參與式的信息檔案化管理:走向全景檔案世界
- 中國人民大學(xué)復(fù)印報(bào)刊資料轉(zhuǎn)載指數(shù)排名研究報(bào)告2017
- 梁思成林徽因影像與手稿珍集
- 金開誠文選