summaryrefslogtreecommitdiff
path: root/rustlings/exercises
diff options
context:
space:
mode:
authorHunter Kvalevog <hunter@kvog.sh>2026-09-18 16:12:53 -0500
committerHunter Kvalevog <hunter@kvog.sh>2026-09-18 16:22:39 -0500
commit84c3c20d440f724cc08b8ef80e16379e84c64cdf (patch)
treec6745162467dc6245044cd1d6a55140e34eefdc5 /rustlings/exercises
parent226a48c93ad9ce05ae7b3075d5b4258eedef8d6e (diff)
rustlings: complete 03
Diffstat (limited to 'rustlings/exercises')
-rw-r--r--rustlings/exercises/03_if/if1.rs10
-rw-r--r--rustlings/exercises/03_if/if2.rs4
-rw-r--r--rustlings/exercises/03_if/if3.rs5
-rw-r--r--rustlings/exercises/quizzes/quiz1.rs10
4 files changed, 17 insertions, 12 deletions
diff --git a/rustlings/exercises/03_if/if1.rs b/rustlings/exercises/03_if/if1.rs
index e5a3c5a..079c671 100644
--- a/rustlings/exercises/03_if/if1.rs
+++ b/rustlings/exercises/03_if/if1.rs
@@ -1,9 +1,9 @@
fn bigger(a: i32, b: i32) -> i32 {
- // TODO: Complete this function to return the bigger number!
- // If both numbers are equal, any of them can be returned.
- // Do not use:
- // - another function call
- // - additional variables
+ if a > b {
+ a
+ } else {
+ b
+ }
}
fn main() {
diff --git a/rustlings/exercises/03_if/if2.rs b/rustlings/exercises/03_if/if2.rs
index ca8493c..8104487 100644
--- a/rustlings/exercises/03_if/if2.rs
+++ b/rustlings/exercises/03_if/if2.rs
@@ -2,8 +2,10 @@
fn picky_eater(food: &str) -> &str {
if food == "strawberry" {
"Yummy!"
+ } else if food == "potato" {
+ "I guess I can eat that."
} else {
- 1
+ "No thanks!"
}
}
diff --git a/rustlings/exercises/03_if/if3.rs b/rustlings/exercises/03_if/if3.rs
index 89164eb..fee96f8 100644
--- a/rustlings/exercises/03_if/if3.rs
+++ b/rustlings/exercises/03_if/if3.rs
@@ -1,13 +1,12 @@
fn animal_habitat(animal: &str) -> &str {
- // TODO: Fix the compiler error in the statement below.
let identifier = if animal == "crab" {
1
} else if animal == "gopher" {
- 2.0
+ 2
} else if animal == "snake" {
3
} else {
- "Unknown"
+ 0
};
// Don't change the expression below!
diff --git a/rustlings/exercises/quizzes/quiz1.rs b/rustlings/exercises/quizzes/quiz1.rs
index 04fb2aa..b787d7e 100644
--- a/rustlings/exercises/quizzes/quiz1.rs
+++ b/rustlings/exercises/quizzes/quiz1.rs
@@ -8,9 +8,13 @@
// - However, if Mary buys more than 40 apples, the price of each apple in the
// entire order is reduced to only 1 rustbuck!
-// TODO: Write a function that calculates the price of an order of apples given
-// the quantity bought.
-// fn calculate_price_of_apples(???) -> ??? { ??? }
+fn calculate_price_of_apples(n: i32) -> i32 {
+ if n > 40 {
+ n
+ } else {
+ 2 * n
+ }
+}
fn main() {
// You can optionally experiment here.