11// use std::io;
22use std:: io:: { self , Write } ;
33
4- fn add ( x : i32 , y : i32 ) -> i32 {
5- x+y // NOTE : In rust no ; means return
4+ fn add ( x : i32 , y : i32 ) -> i32 {
5+ x + y // NOTE : In rust no ; means return
66}
77
8- fn prod ( x : i32 , y : i32 ) -> i32 {
9- return x* y; // can work as well
8+ fn prod ( x : i32 , y : i32 ) -> i32 {
9+ return x * y; // can work as well
1010}
1111
12- fn grade ( score : u32 ) -> & ' static str { // refers to a reference where the borrowed data is valid for the entire duration of the running program
12+ fn grade ( score : u32 ) -> & ' static str {
13+ // refers to a reference where the borrowed data is valid for the entire duration of the running program
1314 match score {
1415 90 ..=100 => "A" ,
1516 80 ..=89 => "B" ,
@@ -19,60 +20,59 @@ fn grade (score : u32) -> &'static str{ // refers to a reference where the borro
1920 }
2021}
2122
22- fn main ( ) -> io:: Result < ( ) > { // this is for safety so that program do not panic
23+ fn main ( ) -> io:: Result < ( ) > {
24+ // this is for safety so that program do not panic
2325 println ! ( "add(5,3) = {}" , add( 5 , 3 ) ) ;
2426 println ! ( "prod(5,3) = {}" , prod( 5 , 3 ) ) ;
2527 println ! ( "grade(95) = {}" , grade( 95 ) ) ;
26-
28+
2729 // Conditional expression
2830 let number = 67 ;
29-
30- if number > 5 {
31+
32+ if number > 5 {
3133 println ! ( "{} is greater than 5" , number) ;
32- } else {
34+ } else {
3335 println ! ( "{} is less than 5 " , number) ;
3436 }
35-
37+
3638 // In Rust you can assign if/else value to a variable
37- let message = if number > 5 { "big" } else { "small" } ;
39+ let message = if number > 5 { "big" } else { "small" } ;
3840 println ! ( "Number is {}" , message) ;
39-
41+
4042 // Loops
4143 let mut count = 0 ;
4244 let result = loop {
4345 count += 1 ;
44- if count == 5 {
45- break count* 2 ;
46+ if count == 5 {
47+ break count * 2 ;
4648 }
4749 } ;
48-
50+
4951 println ! ( "Loop result = {}" , result) ;
50-
52+
5153 count = 0 ;
52- while count <5 {
54+ while count < 5 {
5355 count += 1 ;
54-
5556 }
5657 println ! ( "While loop result = {}" , count) ;
57-
58+
5859 count = 0 ;
5960 for i in 0 ..=5 {
6061 count += i;
6162 }
6263 println ! ( "For loop result = {}" , count) ;
63-
64+
6465 // MATCH
65-
66+
6667 let cmd = "echo" ;
6768 match cmd {
6869 "quit" => println ! ( "Exiting" ) ,
6970 "help" => println ! ( "help" ) ,
7071 "echo" => println ! ( "Hello world" ) ,
7172 _ => println ! ( "Unknown cmd" ) ,
72-
7373 }
74-
75- // MATCH can be used inside a variable
74+
75+ // MATCH can be used inside a variable
7676 let code = 404 ;
7777 let meaning = match code {
7878 200 => "OK" ,
@@ -81,30 +81,30 @@ fn main() -> io::Result<()>{ // this is for safety so that program do
8181 _ => "Unknown" ,
8282 } ;
8383 println ! ( "HTTP {} : {}" , code, meaning) ;
84-
84+
8585 println ! ( "Score 95 = grade {}" , grade( 95 ) ) ;
8686 println ! ( "Score 83 = grade {}" , grade( 83 ) ) ;
8787 println ! ( "Score 60 = grade {}" , grade( 60 ) ) ;
88-
89-
88+
9089 // PART 5 : Reading user input
9190 println ! ( "Type a command (echo / help / exit" ) ;
92-
91+
9392 let mut input = String :: new ( ) ;
94- io:: stdout ( ) . flush ( ) ?; // ensures the prompt apprears before the program waits for input
93+ io:: stdout ( ) . flush ( ) ?; // ensures the prompt apprears before the program waits for input
9594 io:: stdin ( ) . read_line ( & mut input) ?;
96-
97- input= input. trim ( ) . to_string ( ) ;
98-
99- if input. is_empty ( ) {
95+
96+ input = input. trim ( ) . to_string ( ) ;
97+
98+ if input. is_empty ( ) {
10099 println ! ( "Please enter a command" ) ;
101100 } else {
102- match input. as_str ( ) { // when matching against 'input' use as_str() to ensure the type is correct
101+ match input. as_str ( ) {
102+ // when matching against 'input' use as_str() to ensure the type is correct
103103 "echo" => println ! ( "Hello world " ) ,
104104 "help" => println ! ( "Available commands : echo, help, exit" ) ,
105105 "quit" => println ! ( "Exiting ..." ) ,
106106 other => println ! ( "Unknown Command: {}" , other) ,
107107 }
108108 }
109109 Ok ( ( ) )
110- }
110+ }
0 commit comments