Add solutions to functions

This commit is contained in:
mo8it
2024-05-21 02:43:18 +02:00
parent 0f4c42d54e
commit d0b843d6c4
13 changed files with 97 additions and 41 deletions
+1 -1
View File
@@ -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}");
}
+1 -1
View File
@@ -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
+8 -1
View File
@@ -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();
}
+11 -1
View File
@@ -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);
}
+10 -1
View File
@@ -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);
}
+17 -1
View File
@@ -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));
}
+9 -1
View File
@@ -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}");
}