mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-06-15 04:54:17 -06:00
32 lines
769 B
Markdown
32 lines
769 B
Markdown
# Variables
|
|
- Are Immutable by default
|
|
- can be inferred but sometimes needs explicit typing
|
|
|
|
```rust
|
|
let foo = 5;
|
|
```
|
|
- Need to add mut keyword to enable rewriting, generally avoid unless actually used
|
|
|
|
```rust
|
|
let mut bar = 6;
|
|
```
|
|
|
|
# SHADOWING
|
|
|
|
#### Cannot have mutable shadows
|
|
|
|
allows for reuse of namespace instead of spaces_str and spaces_num
|
|
```rust
|
|
let spaces = " _ _ ";
|
|
let spaces = spaces.len();
|
|
// will output 5 instead of " _ _ " beacuse that is how long it is
|
|
// the shadow of spaces (first) wont be printed until the overshadow of spaces goes out of scope
|
|
println!("{spaces}"); // output: 5
|
|
```
|
|
|
|
not allowed shadow
|
|
```rust
|
|
let mut spaces = " _ _ ";
|
|
spaces = spaces.len();
|
|
```
|
|
cannot change type of variable once declared |