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
}
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
}