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
Immutable object
(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 === Rust's [[Rust (programming language)#Ownership|ownership]] system allows developers to declare immutable variables, and pass immutable references. By default, all variables and references are immutable. Mutable variables and references are explicitly created with the <code>mut</code> keyword. [https://doc.rust-lang.org/reference/items/constant-items.html Constant items] in Rust are always immutable. <syntaxhighlight lang="rust"> // constant items are always immutable const ALWAYS_IMMUTABLE: bool = true; struct Object { x: usize, y: usize, } fn main() { // explicitly declare a mutable variable let mut mutable_obj = Object { x: 1, y: 2 }; mutable_obj.x = 3; // okay let mutable_ref = &mut mutable_obj; mutable_ref.x = 1; // okay let immutable_ref = &mutable_obj; immutable_ref.x = 3; // error E0594 // by default, variables are immutable let immutable_obj = Object { x: 4, y: 5 }; immutable_obj.x = 6; // error E0596 let mutable_ref2 = &mut immutable_obj; // error E0596 let immutable_ref2 = &immutable_obj; immutable_ref2.x = 6; // error E0594 } </syntaxhighlight>
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)