Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Reference counting
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Reference counting
(section)
Add topic