typestar

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

  1. const is inlined wherever it is used and needs a type.
  2. static has one address and lives for the whole program.
  3. A type alias is a new name, not a new type.

Keywords and builtins used here

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.

Type this snippet

Step 4 of 4 in Functions, step 26 of 39 in Language basics.

← Previous Next →