Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Reference counting
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Rust=== Like other low-level languages, [[Rust (programming language)|Rust]] does not provide reference counting by default. Instead, any constructed type will be [[Destructor (computer programming)|dropped]] when it falls out of scope. When a programmer needs to define the scope of a constructed type, they often use lifetimes. However, the language also offers various alternatives to complex forms of memory management. Reference counting functionality is provided by the <code>Rc</code> and <code>Arc</code> types, which are non-atomic and atomic respectively. For example, the type <code>Rc<T></code> provides shared ownership of a value of type <code>T</code>, allocated on the heap for multiple references to its data.<ref>{{cite web |title=std::rc - Rust |url=https://doc.rust-lang.org/std/rc/ |access-date=2 November 2020 |website=doc.rust-lang.org}}</ref> <syntaxhighlight lang="rust"> use std::rc::Rc; struct Cat { color: String, } fn main() { let cat = Cat { color: "black".to_string() }; let cat = Rc::new(cat); } </syntaxhighlight>Using these constructs allows programmers to avoid lifetimes for a small runtime cost. Both reference counters keep track of the number of owners, as they must drop themselves when no owners remain. One noteworthy facet of these types is related to their usage as a shared reference. In Rust, shared references cannot mutate their held data, so <code>Rc</code> often comes bundled with <code>Cell</code>, and <code>Arc</code> with <code>Mutex</code>, in contexts where interior mutability is necessary. Interior mutability without <code>UnsafeCell</code> has performance costs, too, so, for maximum performance, some applications may call for additional complexity.<ref>{{Cite web |date=21 July 2022 |title=The Rust Reference |url=https://doc.rust-lang.org/reference/interior-mutability.html |url-status=live |archive-url=https://web.archive.org/web/20240324062327/https://doc.rust-lang.org/reference/types/pointer.html#shared-references- |archive-date=24 March 2024 |access-date=22 April 2024 |at=Interior Mutability}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)