- Rust Programming Cookbook
- Claus Matzinger
- 177字
- 2021-06-24 12:27:51
Shared ownership
Ownership and borrowing are fundamental concepts in Rust; they are the reason no runtime garbage collection is required. As a quick primer: how do they work? In short: scopes. Rust (and many other languages) use (nested) scopes to determine the validity of a variable, so it cannot be used outside of the scope (like a function). In Rust, these scopes own their variables, so they will be gone after the scope finishes. In order for the program to move around values, it can transfer ownership to a nested scope or return it to the parent scope.
For temporary transfers (and multiple viewers), Rust has borrowing, which creates a reference back to the owned value. However, these references are less powerful, and sometimes more complex to maintain (for example, can the reference outlive the original value?), and they are probably the reason why the compiler complains.
In this recipe, we are getting around this problem by sharing ownership using a reference counter that only drops the variable after the counter reaches zero.
- iOS Game Programming Cookbook
- Cocos2D-X權威指南(第2版)
- Visual FoxPro程序設計教程
- Python計算機視覺編程
- Quarkus實踐指南:構建新一代的Kubernetes原生Java微服務
- R Deep Learning Cookbook
- Python全棧數據工程師養成攻略(視頻講解版)
- OpenResty完全開發指南:構建百萬級別并發的Web應用
- Java并發實現原理:JDK源碼剖析
- Offer來了:Java面試核心知識點精講(框架篇)
- Web開發新體驗
- Tkinter GUI Programming by Example
- 計算機常用算法與程序設計教程(第2版)
- 前端Serverless:面向全棧的無服務器架構實戰
- 編程真好玩:從零開始學網頁設計及3D編程