Skip to content

Commit

Permalink
Merge pull request #2290 from dfinity/rust-static-var
Browse files Browse the repository at this point in the history
Update Rust Project Organization doc
  • Loading branch information
jessiemongeon1 authored Dec 20, 2023
2 parents 8c15909 + a03c1ed commit e974224
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions docs/developer-docs/backend/rust/2-project-organization.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,27 +160,17 @@ This definition specifies that the `greet` function is a `query` method which ta

## Declaring global variables

### Stable variables vs flexible variables
### Static variables vs flexible variables

**Stable variables** are global variables that the protocol preserves across upgrades. For example, a user database should probably be stable.
**Static variables** are global variables that the protocol preserves across upgrades. For example, a user database should probably be static.

**Flexible variables** are global variables that the protocol discards on code upgrade. For example, it is reasonable to make a cache flexible if keeping this cache hot is not critical for your product.

### Putting all global variables in one place

It is best practice to store all global variables privately in a single file; the canister main file. This approach is considered the best practice because:

- Testing your code is easier since the majority of your code won't interact with the global variables directly.
- It is easier to understand how the global state is being used by the canister.

It is also recommended that you add comments that within your code that specify which variables are stable, such as:
For example:

```rust
thread_local! {
// static
static USERS: RefCell<Users> = ... ;
// flexible
static LAST_ACTIVE: Cell<UserId> = ...;
}
```

Expand Down

0 comments on commit e974224

Please sign in to comment.