diff options
Diffstat (limited to 'solutions')
| -rw-r--r-- | solutions/01_variables/variables4.rs | 2 | ||||
| -rw-r--r-- | solutions/01_variables/variables5.rs | 2 | ||||
| -rw-r--r-- | solutions/02_functions/functions1.rs | 9 | ||||
| -rw-r--r-- | solutions/02_functions/functions2.rs | 12 | ||||
| -rw-r--r-- | solutions/02_functions/functions3.rs | 11 | ||||
| -rw-r--r-- | solutions/02_functions/functions4.rs | 18 | ||||
| -rw-r--r-- | solutions/02_functions/functions5.rs | 10 |
7 files changed, 57 insertions, 7 deletions
diff --git a/solutions/01_variables/variables4.rs b/solutions/01_variables/variables4.rs index 0540caa..7de6bcb 100644 --- a/solutions/01_variables/variables4.rs +++ b/solutions/01_variables/variables4.rs @@ -4,6 +4,6 @@ fn main() { let mut x = 3; println!("Number {x}"); - x = 5; // Don't change this line + x = 5; println!("Number {x}"); } diff --git a/solutions/01_variables/variables5.rs b/solutions/01_variables/variables5.rs index 456dc9c..9057754 100644 --- a/solutions/01_variables/variables5.rs +++ b/solutions/01_variables/variables5.rs @@ -1,5 +1,5 @@ fn main() { - let number = "T-H-R-E-E"; // Don't change this line + let number = "T-H-R-E-E"; println!("Spell a number: {}", number); // Using variable shadowing diff --git a/solutions/02_functions/functions1.rs b/solutions/02_functions/functions1.rs index 4e18198..dc52744 100644 --- a/solutions/02_functions/functions1.rs +++ b/solutions/02_functions/functions1.rs @@ -1 +1,8 @@ -// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 +// Some function with the name `call_me` without arguments or a return value. +fn call_me() { + println!("Hello world!"); +} + +fn main() { + call_me(); +} diff --git a/solutions/02_functions/functions2.rs b/solutions/02_functions/functions2.rs index 4e18198..f14ffa3 100644 --- a/solutions/02_functions/functions2.rs +++ b/solutions/02_functions/functions2.rs @@ -1 +1,11 @@ -// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 +// The type of function arguments must be annotated. +// Added the type annotation `u64`. +fn call_me(num: u64) { + for i in 0..num { + println!("Ring! Call number {}", i + 1); + } +} + +fn main() { + call_me(3); +} diff --git a/solutions/02_functions/functions3.rs b/solutions/02_functions/functions3.rs index 4e18198..c581c42 100644 --- a/solutions/02_functions/functions3.rs +++ b/solutions/02_functions/functions3.rs @@ -1 +1,10 @@ -// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 +fn call_me(num: u32) { + for i in 0..num { + println!("Ring! Call number {}", i + 1); + } +} + +fn main() { + // `call_me` expects an argument. + call_me(5); +} diff --git a/solutions/02_functions/functions4.rs b/solutions/02_functions/functions4.rs index 4e18198..f823de2 100644 --- a/solutions/02_functions/functions4.rs +++ b/solutions/02_functions/functions4.rs @@ -1 +1,17 @@ -// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 +fn is_even(num: i64) -> bool { + num % 2 == 0 +} + +// The return type must always be annotated. +fn sale_price(price: i64) -> i64 { + if is_even(price) { + price - 10 + } else { + price - 3 + } +} + +fn main() { + let original_price = 51; + println!("Your sale price is {}", sale_price(original_price)); +} diff --git a/solutions/02_functions/functions5.rs b/solutions/02_functions/functions5.rs index 4e18198..0335418 100644 --- a/solutions/02_functions/functions5.rs +++ b/solutions/02_functions/functions5.rs @@ -1 +1,9 @@ -// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 +fn square(num: i32) -> i32 { + // Removed the semicolon `;` at the end of the line below to implicitely return the result. + num * num +} + +fn main() { + let answer = square(3); + println!("The square of 3 is {answer}"); +} |
