Constants and type aliases in Rust
Compile-time constants, program-lifetime statics, and names for long types.
const MAX_STARS: u32 = 3;
const GREETING: &str = "welcome";
static VERSION: &str = "2.1.0";
type Scores = Vec<(String, u32)>;
fn main() {
println!("{GREETING} v{VERSION}, up to {MAX_STARS} stars");
let mut scores: Scores = Vec::new();
scores.push(("ada".to_string(), 3));
scores.push(("grace".to_string(), 2));
println!("{:?}", scores);
}
How it works
constis inlined wherever it is used and needs a type.statichas one address and lives for the whole program.- A
typealias is a new name, not a new type.
Keywords and builtins used here
ScoresStringVecconstfnletmainmutstaticstrtypeu32
The run, in numbers
- Lines
- 14
- Characters to type
- 341
- Tokens
- 107
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 71 seconds.
Step 4 of 4 in Functions, step 26 of 39 in Language basics.