Rust memory management: Difference between revisions
From wikinotes
Line 31: | Line 31: | ||
* Objects have a single owner at once | * Objects have a single owner at once | ||
* When owner goes out of scope, value is dropped (with <code>drop()</code>) | * When owner goes out of scope, value is dropped (with <code>drop()</code>) | ||
* When an object is passed as a function-parameter, that function now owns it (and it cannot be referenced in current context). | |||
See [https://doc.rust-lang.org/stable/book/ch04-01-what-is-ownership.html#ownership-and-functions example] of ownership in action. | See [https://doc.rust-lang.org/stable/book/ch04-01-what-is-ownership.html#ownership-and-functions example] of ownership in action. | ||
</blockquote><!-- Ownership --> | </blockquote><!-- Ownership --> | ||
</blockquote><!-- General --> | </blockquote><!-- General --> |
Revision as of 15:52, 7 February 2023
Rust uses ownership semantics for memory management.
Documentation
official tutorial https://doc.rust-lang.org/stable/book/ch04-00-understanding-ownership.html
General
Stack
The stack is
- a LIFO
- push=add, pop=remove (from the top)
- only supports fixed-size datatypes
- fast
Heap
- access provided through pointers (a fixed-size, usable on stack)
- slower
Ownership
- Objects have a single owner at once
- When owner goes out of scope, value is dropped (with
drop()
)- When an object is passed as a function-parameter, that function now owns it (and it cannot be referenced in current context).
See example of ownership in action.