From 521c375edddcdc5a3fa2eaf861f3b450d06d064d Mon Sep 17 00:00:00 2001 From: Per Lindgren <per.lindgren@ltu.se> Date: Thu, 17 Sep 2020 11:16:46 +0200 Subject: [PATCH] examples from student repo --- examples/w1_2.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 examples/w1_2.rs diff --git a/examples/w1_2.rs b/examples/w1_2.rs new file mode 100644 index 0000000..fcf5cd9 --- /dev/null +++ b/examples/w1_2.rs @@ -0,0 +1,62 @@ +// 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() {} -- GitLab