Skip to content

Commit 1235a11

Browse files
functions, loops & user input
1 parent ea0e536 commit 1235a11

3 files changed

Lines changed: 120 additions & 2 deletions

File tree

examples/01_variables.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ fn main() {
5050

5151
// PART5 : Constants
5252

53-
const euler_num: f32 = 2.71828;
54-
println!("Max euler_num= {}", euler_num );
53+
const EULER_NUM: f32 = 2.71828;
54+
println!("Max euler_num= {}", EULER_NUM );
5555

5656

5757

examples/02_functions.rs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// TO RUN THE FILE : cargo run --example 02_functions
2+
// use std::io;
3+
use std::io::{self, Write};
4+
5+
fn add(x: i32, y: i32) -> i32{
6+
x+y // NOTE : In rust no ; means return
7+
}
8+
9+
fn prod(x: i32, y: i32) -> i32{
10+
return x*y; // can work as well
11+
}
12+
13+
fn grade (score : u32) -> &'static str{ // refers to a reference where the borrowed data is valid for the entire duration of the running program
14+
match score {
15+
90..=100 => "A",
16+
80..=89 => "B",
17+
70..=79 => "C",
18+
60..=69 => "D",
19+
_ => "F",
20+
}
21+
}
22+
23+
fn main() -> io::Result<()>{ // this is for safety so that program do not panic
24+
println!("add(5,3) = {}", add(5, 3));
25+
println!("prod(5,3) = {}", prod(5, 3));
26+
println!("grade(95) = {}", grade(95));
27+
28+
// Conditional expression
29+
let number = 67;
30+
31+
if number > 5{
32+
println!("{} is greater than 5", number);
33+
}else{
34+
println!("{} is less than 5 ", number);
35+
}
36+
37+
// In Rust you can assign if/else value to a variable
38+
let message = if number > 5{"big"} else {"small"};
39+
println!("Number is {}", message);
40+
41+
// Loops
42+
let mut count = 0;
43+
let result = loop {
44+
count += 1;
45+
if count == 5{
46+
break count*2;
47+
}
48+
};
49+
50+
println!("Loop result = {}", result);
51+
52+
count = 0;
53+
while count <5{
54+
count += 1;
55+
56+
}
57+
println!("While loop result = {}", count);
58+
59+
count = 0;
60+
for i in 0..=5 {
61+
count += i;
62+
}
63+
println!("For loop result = {}", count);
64+
65+
// MATCH
66+
67+
let cmd = "echo";
68+
match cmd {
69+
"quit" => println!("Exiting"),
70+
"help" => println!("help"),
71+
"echo" => println!("Hello world"),
72+
_ => println!("Unknown cmd"),
73+
74+
}
75+
76+
// MATCH can be used inside a variable
77+
let code = 404;
78+
let meaning = match code {
79+
200 => "OK",
80+
404 => "Not Found",
81+
500 => "Server Error",
82+
_ => "Unknown",
83+
};
84+
println!("HTTP {} : {}", code, meaning);
85+
86+
println!("Score 95 = grade {}", grade(95));
87+
println!("Score 83 = grade {}", grade(83));
88+
println!("Score 60 = grade {}", grade(60));
89+
90+
91+
// PART 5 : Reading user input
92+
println!("Type a command (echo / help / exit");
93+
94+
let mut input = String::new();
95+
io::stdout().flush()?; // ensures the prompt apprears before the program waits for input
96+
io::stdin().read_line(&mut input)?;
97+
98+
input= input.trim().to_string();
99+
100+
if input.is_empty(){
101+
println!("Please enter a command");
102+
} else {
103+
match input.as_str() { // when matching against 'input' use as_str() to ensure the type is correct
104+
"echo" => println!("Hello world "),
105+
"help" => println!("Available commands : echo, help, exit"),
106+
"quit" => println!("Exiting ..."),
107+
other => println!("Unknown Command: {}", other),
108+
}
109+
}
110+
Ok(())
111+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use std::io;
2+
3+
fn main() -> io::Result<()>{
4+
5+
6+
Ok(())
7+
}

0 commit comments

Comments
 (0)