Skip to content

Commit

Permalink
basic-types: add slide on static/const
Browse files Browse the repository at this point in the history
Worth getting it out of the way up front because trainees will see this syntax when looking at codebases on-line, and it doesn't come up anywhere else.
  • Loading branch information
jonathanpallant committed Oct 15, 2024
1 parent 23d5288 commit 63bc794
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions training-slides/src/basic-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ Rust comes with all standard int types, with and without sign
* `i64`, `u64`
* `i128`, `u128`

## Kinds of variable

```rust
static X: i32 = 42;
const Y: i32 = 42;

fn some_function() {
let x = 42;
let x: i32 = 42;
let mut x = 42;
let mut x: i32 = 42;
}
```

Note:

The expression used to initialise a `static` or `const` must be evaluatable at compile time. This includes calling `const fn` functions. A `let` binding doesn't have this restriction.

The `static` occupies some memory at run-time and get a symbol in the symbol table. The `const` does not, and is only used to initialise other values (or e.g. as an argument to a function) - it acts a bit like a C pre-processor macro.

## Syntactic clarity in specifying numbers

```rust
Expand Down

0 comments on commit 63bc794

Please sign in to comment.