summaryrefslogtreecommitdiff
path: root/solutions/02_functions
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 /solutions/02_functions
parent0f4c42d54ea7322a4ee0ae7036c058c3061e80e9 (diff)
Add solutions to functions
Diffstat (limited to 'solutions/02_functions')
-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
5 files changed, 55 insertions, 5 deletions
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}");
+}