summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-05-21 02:43:18 +0200
committermo8it <mo8it@proton.me>2024-05-21 02:43:18 +0200
commitd0b843d6c4a99636d3dc6caf3ceebea14cb3b07d (patch)
tree2021b096bdd07d37abbf71ed1df3b0b58753f67a
parent0f4c42d54ea7322a4ee0ae7036c058c3061e80e9 (diff)
Add solutions to functions
-rw-r--r--exercises/02_functions/functions1.rs4
-rw-r--r--exercises/02_functions/functions2.rs9
-rw-r--r--exercises/02_functions/functions3.rs9
-rw-r--r--exercises/02_functions/functions4.rs19
-rw-r--r--exercises/02_functions/functions5.rs11
-rw-r--r--rustlings-macros/info.toml22
-rw-r--r--solutions/01_variables/variables4.rs2
-rw-r--r--solutions/01_variables/variables5.rs2
-rw-r--r--solutions/02_functions/functions1.rs9
-rw-r--r--solutions/02_functions/functions2.rs12
-rw-r--r--solutions/02_functions/functions3.rs11
-rw-r--r--solutions/02_functions/functions4.rs18
-rw-r--r--solutions/02_functions/functions5.rs10
13 files changed, 97 insertions, 41 deletions
diff --git a/exercises/02_functions/functions1.rs b/exercises/02_functions/functions1.rs
index 4e3b103..a812c21 100644
--- a/exercises/02_functions/functions1.rs
+++ b/exercises/02_functions/functions1.rs
@@ -1,3 +1,5 @@
+// TODO: Add some function with the name `call_me` without arguments or a return value.
+
fn main() {
- call_me();
+ call_me(); // Don't change this line
}
diff --git a/exercises/02_functions/functions2.rs b/exercises/02_functions/functions2.rs
index 84e09cd..2c773c6 100644
--- a/exercises/02_functions/functions2.rs
+++ b/exercises/02_functions/functions2.rs
@@ -1,9 +1,10 @@
-fn main() {
- call_me(3);
-}
-
+// TODO: Add the missing type of the argument `num` after the colon `:`.
fn call_me(num:) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
}
+
+fn main() {
+ call_me(3);
+}
diff --git a/exercises/02_functions/functions3.rs b/exercises/02_functions/functions3.rs
index 66fb6d3..5d5122a 100644
--- a/exercises/02_functions/functions3.rs
+++ b/exercises/02_functions/functions3.rs
@@ -1,9 +1,10 @@
-fn main() {
- call_me();
-}
-
fn call_me(num: u32) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
}
+
+fn main() {
+ // TODO: Fix the function call.
+ call_me();
+}
diff --git a/exercises/02_functions/functions4.rs b/exercises/02_functions/functions4.rs
index 06d3b1b..b22bffd 100644
--- a/exercises/02_functions/functions4.rs
+++ b/exercises/02_functions/functions4.rs
@@ -1,14 +1,14 @@
// This store is having a sale where if the price is an even number, you get 10
-// Rustbucks off, but if it's an odd number, it's 3 Rustbucks off. (Don't worry
-// about the function bodies themselves, we're only interested in the signatures
-// for now. If anything, this is a good way to peek ahead to future exercises!)
+// Rustbucks off, but if it's an odd number, it's 3 Rustbucks off.
+// Don't worry about the function bodies themselves, we are only interested in
+// the signatures for now.
-fn main() {
- let original_price = 51;
- println!("Your sale price is {}", sale_price(original_price));
+fn is_even(num: i64) -> bool {
+ num % 2 == 0
}
-fn sale_price(price: i32) -> {
+// TODO: Fix the function signature.
+fn sale_price(price: i64) -> {
if is_even(price) {
price - 10
} else {
@@ -16,6 +16,7 @@ fn sale_price(price: i32) -> {
}
}
-fn is_even(num: i32) -> bool {
- num % 2 == 0
+fn main() {
+ let original_price = 51;
+ println!("Your sale price is {}", sale_price(original_price));
}
diff --git a/exercises/02_functions/functions5.rs b/exercises/02_functions/functions5.rs
index 3bb5e52..31fd057 100644
--- a/exercises/02_functions/functions5.rs
+++ b/exercises/02_functions/functions5.rs
@@ -1,8 +1,9 @@
-fn main() {
- let answer = square(3);
- println!("The square of 3 is {}", answer);
-}
-
+// TODO: Fix the function body without chaning the signature.
fn square(num: i32) -> i32 {
num * num;
}
+
+fn main() {
+ let answer = square(3);
+ println!("The square of 3 is {answer}");
+}
diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml
index be3b262..495e9c3 100644
--- a/rustlings-macros/info.toml
+++ b/rustlings-macros/info.toml
@@ -142,7 +142,7 @@ test = false
hint = """
This `main` function is calling a function that it expects to exist, but the
function doesn't exist. It expects this function to have the name `call_me`.
-It expects this function to not take any arguments and not return a value.
+It also expects this function to not take any arguments and not return a value.
Sounds a lot like `main`, doesn't it?"""
[[exercises]]
@@ -159,7 +159,7 @@ dir = "02_functions"
test = false
hint = """
This time, the function *declaration* is okay, but there's something wrong
-with the place where we're calling the function."""
+with the place where we are calling the function."""
[[exercises]]
name = "functions4"
@@ -167,8 +167,8 @@ dir = "02_functions"
test = false
hint = """
The error message points to the function `sale_price` and says it expects a type
-after the `->`. This is where the function's return type should be -- take a
-look at the `is_even` function for an example!"""
+after `->`. This is where the function's return type should be.
+Take a look at the `is_even` function for an example!"""
[[exercises]]
name = "functions5"
@@ -177,15 +177,15 @@ test = false
hint = """
This is a really common error that can be fixed by removing one character.
It happens because Rust distinguishes between expressions and statements:
-expressions return a value based on their operand(s), and statements simply
-return a `()` type which behaves just like `void` in C/C++ language.
+Expressions return a value based on their operand(s), and statements simply
+return a `()` type which behaves just like `void` in C/C++.
-We want to return a value of `i32` type from the `square` function, but it is
-returning a `()` type...
+We want to return a value with the type `i32` from the `square` function, but
+it is returning the type `()`.
-They are not the same. There are two solutions:
-1. Add a `return` ahead of `num * num;`
-2. remove `;`, make it to be `num * num`"""
+There are two solutions:
+1. Add the `return` keyword before `num * num;`
+2. Remove the semicolon `;` after `num * num`"""
# IF
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}");
+}