mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-03-26 07:52:20 +02:00
840 B
840 B
Static and Constant Variables
Global state is managed with static and constant variables.
const
You can declare compile-time constants:
const DIGEST_SIZE: usize = 3;
const ZERO: Option<u8> = Some(42);
fn compute_digest(text: &str) -> [u8; DIGEST_SIZE] {
let mut digest = [ZERO.unwrap_or(0); DIGEST_SIZE];
for (idx, &b) in text.as_bytes().iter().enumerate() {
digest[idx % DIGEST_SIZE] = digest[idx % DIGEST_SIZE].wrapping_add(b);
}
digest
}
fn main() {
let digest = compute_digest("Hello");
println!("Digest: {digest:?}");
}
static
You can also declare static variables:
static BANNER: &str = "Welcome to RustOS 3.14";
fn main() {
println!("{BANNER}");
}
We will look at mutating static data in the chapter on Unsafe Rust.