Constants and statics - Rust for C-Programmers
Constants represent values that are known at compile time. They must be annotated with a type and are typically defined in the global scope, though they can also be defined within functions. Constants are effectively inlined wherever they are used and do not have a fixed memory address. The naming convention is SCREAMING_SNAKE_CASE. Static variables represent values that have a fixed memory location ('static lifetime) throughout the program’s execution. They are initialized once, usually when the program starts. Like constants, they must have an explicit type annotation. The naming convention is also SCREAMING_SNAKE_CASE. Rust strongly discourages mutable static variables (static mut) because modifying global state without synchronization can easily lead to data races in concurrent code. Accessing or modifying static mut variables requires unsafe blocks.
Keyboard shortcuts Press ← or → to navigate between chapters Press S or / to search in the book Press ? to show this help Press Esc to hide this help 2.6 Constants and Static Variables Rust offers two ways to define values with fixed meaning or location: 2.6.1 Constants (const) Constants represent values that are known at compile time. They must be annotated with a type and are typically defined in the global scope, though they can also be defined within functions. Constants are effectively inlined wherever they are used and do not have a fixed memory address. The naming convention…
saved by
related reading
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- Data types - Rust for C-Programmersrust-for-c-programmers.com
- The program structure - Rust for C-Programmersrust-for-c-programmers.com
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- Memory safety and ownership - Rust for C-Programmersrust-for-c-programmers.com
- Rust for C programmers - Rust for C-Programmersrust-for-c-programmers.com
- Modules and crates - Rust for C-Programmersrust-for-c-programmers.com
- Variables and Mutability - The Rust Programming Languagedoc.rust-lang.org
- What makes Rust special - Rust for C-Programmersrust-for-c-programmers.com
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- Moves, cloning, and copying - Rust for C-Programmersrust-for-c-programmers.com