Professional Documents
Culture Documents
> function that follows `::` is called an associated function of the class before
`::`.
### Constants
differs from immutable variables:
- can't use `mut` with `const`
- const are always immutable
- the type of const MUST be annotated
- set to constant expression (can't be calculated at runtime depending on other
vars. can do primitive calcs like `60*3`)
- can be declared in any scope (incl global)
### Shadowing
Can create var with the same name, so the program will see the new value. In that
case they say the old value was shadowed by the new.
By shadowing we can change the type of variable as well to avoid having names
`spaces_string`, `spaces_num`. But when trying to use mutable variable assigning to
the same var different types would throw and error.
```rust
fn main() {
let x = 5;
let x = x + 1;
{
let x = x * 2;
println!("The value of x in the inner scope is: {}", x); // 12
}
```rust
let mut spaces = " ";
spaces = spaces.len(); // expected `&str`, found `usize`
// not allowed to mutate a variable’s type
```
## Data Types
Every value has a type. Rust is `statically typed` = it must know the types of all
variables at compile time. The compiler guesses the type, but in cases of ambiguity
it requires type annotation.