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 {
@dbg(0); // zero
@dbg(42); // positive integer
@dbg(255); // maximum u8 value
0
}
Float Literals
A float literal (2.1:29) evaluates to the floating-point value obtained by rounding its exact written decimal value to the nearest value of its type, ties to even (3.12:9).
A float literal has type comptime_float and takes a concrete floating-point type from its context; with no such context its type is f64 (3.12:7, 3.12:8). Unlike an integer literal it is never given an integer type, however integral its value.
fn main() -> i32 {
let a = 2.5; // f64 by default
let b: f32 = 2.5; // f32 by annotation
@dbg(a);
@dbg(b);
0
}
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. Without a contextual text type, its type is the stable core str view; a context may promote it to another text rung such as Str(N) or the explicitly imported standard-library StrBuf.
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
}