Rust variables: Difference between revisions
From wikinotes
Line 27: | Line 27: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Constants --> | </blockquote><!-- Constants --> | ||
= Mutability = | |||
<blockquote> | |||
All variables are immutable by default in rust.<br> | |||
You can make them mutable with the <code>mut</code> modifier. | |||
<syntaxhighlight lang="rust"> | |||
let mut name: &str = "Alex"; | |||
</syntaxhighlight> | |||
</blockquote><!-- Mutability --> | |||
= Introspection = | = Introspection = |
Revision as of 00:08, 7 September 2021
Assignment
let age: u8 = 200; // typed let age = 200u8; // type-suffix let age = 200; // implied type
Literals
Literal types can be declared without assigning a type.
let float = 3.14; // f64 let integer = 7; // i32
Constants
Constants can be declared in any scope (including global).
constants cannot be changed once assigned.const SALT: &str = "$6$r1ohStL5/UwpNnls"; static RETRIES: i8 = 5;
Mutability
All variables are immutable by default in rust.
You can make them mutable with themut
modifier.let mut name: &str = "Alex";
Introspection
TODO:
learn
// use std::any::type_name; (todo - learn)