- 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.
- Unity 2020 By Example
- Vue.js 3.x快速入門
- Hands-On Machine Learning with scikit:learn and Scientific Python Toolkits
- Django開發(fā)從入門到實(shí)踐
- iOS開發(fā)實(shí)戰(zhàn):從零基礎(chǔ)到App Store上架
- SQL語言從入門到精通
- 區(qū)塊鏈:以太坊DApp開發(fā)實(shí)戰(zhàn)
- Mastering C# Concurrency
- Learning Neo4j 3.x(Second Edition)
- Big Data Analytics
- Kotlin從基礎(chǔ)到實(shí)戰(zhàn)
- Multithreading in C# 5.0 Cookbook
- 51單片機(jī)C語言開發(fā)教程
- ASP.NET開發(fā)與應(yīng)用教程
- MySQL入門很輕松(微課超值版)