int
- 64 bit signed integer
float
- 64 bit floating point
let variable: float = 2;
// or
let variable = 2.0;
let variable: bool = true;
variable = false;
let variable: str = "hello world";
=
- assign a value to a variable
let variable = 2;
variable = 3;
const
- declare a constant
fn main() {
println("hello world");
}
fn add(a: int, b: int) {
println(a + b);
}
->
- function return type
fn add(a: int, b: int) -> int {
return a + b;
}
let variable = 2;
if variable == 2 {
println("variable is 2");
} else {
println("variable is not 2");
}
let variable = 2;
match variable {
1 => println("variable is 1"),
2 => println("variable is 2"),
_ => println("variable is not 1 or 2"),
}
let variable = 0;
loop {
println(variable);
variable += 1;
if variable == 10 {
break;
}
}
let variable = 0;
while variable < 10 {
println(variable);
variable += 1;
}
for variable in 0..10 {
println(variable);
}
Comments
// this is a single line comment
/* */
- multi line comment
/*
this is a multi line comment
*/
+
- addition
-
- subtraction
*
- multiplication
/
- division
%
- modulus
==
- equal
!=
- not equal
>
- greater than
<
- less than
>=
- greater than or equal
<=
- less than or equal
&
- bitwise and
|
- bitwise or
^
- bitwise xor
~
- bitwise not
<<
- bitwise left shift
>>
- bitwise right shift
=
- assign a value to a variable
+=
- add and assign
-=
- subtract and assign
*=
- multiply and assign
/=
- divide and assign
%=
- modulus and assign
&=
- bitwise and and assign
|=
- bitwise or and assign
^=
- bitwise xor and assign
<<=
- bitwise left shift and assign
>>=
- bitwise right shift and assign
let array: int[] = [1, 2, 3];
let array = [1, 2, 3];
println(array[0]); // 1
let array = [1, 2, 3];
println(array[0..2]); // [1, 2]
struct Person {
name: str,
age: int,
}
let person: Person = {
name: "John",
age: 20,
};
()
- declare a struct with a constructor
struct Person(str, int);
let person = Person("John", 20);
let person: Person = {
name: "John",
age: 20,
};
println(person.name); // John
enum Color {
Red,
Green,
Blue,
}
let color = Color::Red;
println(color); // Red
mod module_name {
// module code
}
use file_name::module_name;
println
- print to the console
print
- print to the console without a newline