Comments are used to document the code and are ignored by the compiler. Inference supports double-slash comments // and tripple-slash /// docstrings interpreted also as comments.
/// This is a docstring
// This is a line comment
fnexternalletmutforallexists@assumeuniqueloopreturnfromusepubmodifbreakelsestructenumtypespecconstassertself
An identifier is a sequence of characters that is used to name variables, functions, and other entities in the program. Identifiers must start with a letter or an underscore and can contain letters, digits, and underscores.
let a: u32 = 42;
let _ident : i64 = 42;
constructorprooftypeofuzumaki
A qualified identifier is a sequence of identifiers separated by ::. It is used to refer to functions in structs and contexts.
let a: spec::AuctionSpec = spec::AuctionSpec::new();
The member access operator . is used to access fields and methods of a struct.
struct Account {
address: i32;
fn new(addr: i32) -> Account {
return Account { address: addr };
}
}
fn main() {
let a: Account = Account::new(42);
let b: i32 = a.address;
}
bool is a boolean type that can have one of two values: true or false.
let a: bool = true;
let b: bool = false;
unit is a type that has only one value: (). It is used to represent the absence of a value.
let a: unit = ();
A numeric literal is a sequence of decimal digits, optionally preceded by the unary - sign for negation. The literal's type is determined by the context — typically the explicit type annotation on the surrounding declaration or the expected type of the surrounding expression.
let a: i32 = 42;
let b: u64 = 1000;
let c: i32 = -7;
The right arrow -> is used to specify the return type of a function.
fn add(a: u32, b: u32) -> u32 {
return a + b;
}
The terminator ; is used to separate statements in Inference.
let a: u32 = 42;
Curly braces {} are used to define blocks of code.
See also: Statements
fn foo() {
/// code block
}
Parentheses () are used to group expressions and arguments in function calls. Also a () outside of a function definition spec is interpreted as a single token and is used to represent the unit type.
See also: Functions See also: Types
fn foo(a: u32, b: u32) -> u32 {
return a + b;
}
let a: () = ();
Square brackets [] are used to define arrays and address individual elements of an array.
See also: Types
let a: [u32; 3] = [1, 2, 3];
let b: u32 = a[0];
Prime symbol ' is used to define type parameters.
See also: Types
fn foo T' (a: T') {
// code block
}
The pub keyword marks a top-level definition as exported from the compiled module. Items without pub are private to the compilation unit and may be referenced only from inside the same source file. Spec functions are never exported even when declared pub — see §10.5 Spec.
pub is currently accepted on fn, struct, and enum definitions.
pub fn add(a: i32, b: i32) -> i32 {
return a + b;
}
pub struct Point {
x: i32;
y: i32;
}
pub enum Color {
Red,
Green,
Blue,
}