Skip to content

Commit 0b2af4d

Browse files
committed
Adding functions, loops and variables examples
1 parent 10f3e37 commit 0b2af4d

File tree

6 files changed

+149
-0
lines changed

6 files changed

+149
-0
lines changed

functions/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "functions"
3+
version = "0.1.0"
4+
authors = ["Chanaka Fernando <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

functions/src/main.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
4+
another_function(50);
5+
6+
println!("Value from five() function is {}", five());
7+
8+
let x = plus_one(5);
9+
10+
println!("The value of x is: {}", x);
11+
12+
// If condition check
13+
let x = 3;
14+
if x < 5 {
15+
println!("Condition is true!")
16+
} else {
17+
println!("Condition is false!");
18+
}
19+
}
20+
21+
fn plus_one(x: i32) -> i32 {
22+
x + 1
23+
}
24+
25+
fn another_function(x :i32) {
26+
println!("Another function. value of x is {}", x);
27+
}
28+
29+
fn five() -> i32 {
30+
5
31+
}
32+
33+

loops/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "loops"
3+
version = "0.1.0"
4+
authors = ["Chanaka Fernando <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

loops/src/main.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
fn main() {
2+
println!("Hello, loops!");
3+
let mut counter = 0;
4+
5+
let result = loop {
6+
counter += 1;
7+
8+
if counter == 10 {
9+
break counter * 5;
10+
}
11+
};
12+
13+
println!("Value returned from loop is {}", result);
14+
while_test();
15+
loop_collection();
16+
for_loop();
17+
for_loop_range();
18+
19+
}
20+
21+
fn while_test() {
22+
println!("Staring the while loop");
23+
let mut x = 3;
24+
while x != 0 {
25+
println!("{}!", x);
26+
x -= 1;
27+
}
28+
println!("LIFTOFF!!!");
29+
}
30+
31+
fn loop_collection() {
32+
println!("Starting the looping through the collection");
33+
let a = [10,20,30,40,50];
34+
let mut index = 0;
35+
36+
while index < 5 {
37+
println!("the value is {}", a[index]);
38+
index += 1;
39+
}
40+
println!("LIFTOFF!!!");
41+
}
42+
43+
fn for_loop() {
44+
println!("Staring the for loop test");
45+
let a = [1,2,3,4,5];
46+
47+
for element in a.iter() {
48+
println!("the value is {}!", element);
49+
}
50+
println!("LIFTOFF!!!");
51+
}
52+
53+
fn for_loop_range() {
54+
println!("Staring the for loop with range test");
55+
for number in (1..4).rev() {
56+
println!("the value is {}!", number);
57+
}
58+
println!("LIFTOFF!!!");
59+
}
60+
61+

variables/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "variables"
3+
version = "0.1.0"
4+
authors = ["Chanaka Fernando <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

variables/src/main.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
fn main() {
2+
println!("Hello, variables!");
3+
let mut x = 5;
4+
println!("Value of x is {}", x);
5+
x = 6;
6+
println!("Value of x is now {}", x);
7+
8+
let x = 5;
9+
10+
let x = x + 1;
11+
12+
let x = x * 2;
13+
14+
println!("The value of x is: {}", x);
15+
16+
toupleLearn();
17+
}
18+
19+
fn toupleLearn() {
20+
21+
println!("Learning tuples in Rust");
22+
23+
let tup = (500, 6.4, 1);
24+
25+
let (x, y, z) = tup;
26+
27+
println!("The value of y is: {}", y);
28+
}

0 commit comments

Comments
 (0)