Skip to content
Snippets Groups Projects
Commit 521c375e authored by Per Lindgren's avatar Per Lindgren
Browse files

examples from student repo

parent 7e92be3d
Branches
No related tags found
No related merge requests found
// a function taking no arguments returning the unit type
fn a() -> () {
let _a: i32 = 5; // this returns a unit type
}
// a function taking two i32 arguments returning the i32 type
fn b(_x: i32, _y: i32) -> i32 {
3 // this returns 3 (as i32)
}
// a function taking two i32 arguments returning the i32 type
// with some let statements
fn c(x: i32, y: i32) -> i32 {
let a: i32 = 5;
let b: i32 = x + y; // this will be an infix operator "+""
-a - (-b) * y // here we have prefix operator "-"
}
// More advanced statements
// a function taking two bool arguments returning the bool type
// with some let statements and function calls
fn a(x: bool, y: bool) -> bool {
if x && y {
let a: bool = true;
y || a
} else {
x && false
}
}
// a function taking two bool arguments returning the i32 type
// with some let statements and function calls
fn b(x: bool, y: bool) -> i32 {
let a: bool = a(x, y || false);
let mut b: i32 = 0;
if a && y {
let a: bool = true; // shadowing
if y || a {
b = b + 1;
};
} else {
if !(x && false) {
b = b - 1;
}
};
b + 3
}
// a function taking two bool arguments returning the i32 type
// while
fn c(x: bool, y: bool) -> i32 {
let mut b: i32 = 0;
let mut c: i32 = 1;
while (b < 10) {
c = c * 2;
};
c
}
fn main() {}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment