Literal Expressions

A literal expression evaluates to a constant value.

Integer Literals

An integer literal is a sequence of decimal digits that evaluates to an integer value.

Integer literals default to type i32 unless the context requires a different type.

fn main() -> i32 {
    0       // zero
    42      // positive integer
    255     // maximum u8 value
}

Boolean Literals

The boolean literals are true and false, both of type bool.

fn main() -> i32 {
    let a = true;
    let b = false;
    if a { 1 } else { 0 }
}

Unit Literal

The unit literal () is an expression of type ().

The unit literal evaluates to the single value of the unit type.

fn returns_unit() -> () {
    ()
}

fn main() -> i32 {
    let u = ();
    returns_unit();
    0
}

String Literals

A string literal is a sequence of characters enclosed in double quotes, of type String.

String literals support escape sequences: \\ for a backslash and \" for a double quote.

fn main() -> i32 {
    let a = "hello";
    let b = "world";
    let c = "with \"quotes\"";
    0
}