Make starting fireworks more fun :)

This commit is contained in:
Remo Senekowitsch
2026-04-13 18:58:53 +02:00
parent 7c1d8ebf49
commit 346753b673
2 changed files with 14 additions and 28 deletions
+7 -14
View File
@@ -22,13 +22,7 @@ fn add_rockets(fireworks: &mut Fireworks, rockets: usize) {
// TODO: Turn this function into a method on `Fireworks`.
fn start(fireworks: Fireworks) -> String {
if fireworks.rockets < 5 {
String::from("small")
} else if fireworks.rockets < 20 {
String::from("medium")
} else {
String::from("big")
}
"🚀".repeat(fireworks.rockets)
}
fn main() {
@@ -41,18 +35,17 @@ mod tests {
#[test]
fn start_some_fireworks() {
let f = Fireworks::new();
assert_eq!(f.start(), "");
let mut f = Fireworks::new();
f.add_rockets(3);
assert_eq!(f.start(), "small");
assert_eq!(f.start(), "🚀🚀🚀");
let mut f = Fireworks::new();
f.add_rockets(15);
assert_eq!(f.start(), "medium");
let mut f = Fireworks::new();
f.add_rockets(100);
f.add_rockets(7);
// We don't use method syntax in the last test to ensure the `start`
// function takes ownership of the fireworks.
assert_eq!(Fireworks::start(f), "big");
assert_eq!(Fireworks::start(f), "🚀🚀🚀🚀🚀🚀🚀");
}
}
+7 -14
View File
@@ -15,13 +15,7 @@ impl Fireworks {
}
fn start(self) -> String {
if self.rockets < 5 {
String::from("small")
} else if self.rockets < 20 {
String::from("medium")
} else {
String::from("big")
}
"🚀".repeat(self.rockets)
}
}
@@ -35,18 +29,17 @@ mod tests {
#[test]
fn start_some_fireworks() {
let f = Fireworks::new();
assert_eq!(f.start(), "");
let mut f = Fireworks::new();
f.add_rockets(3);
assert_eq!(f.start(), "small");
assert_eq!(f.start(), "🚀🚀🚀");
let mut f = Fireworks::new();
f.add_rockets(15);
assert_eq!(f.start(), "medium");
let mut f = Fireworks::new();
f.add_rockets(100);
f.add_rockets(7);
// We don't use method syntax in the last test to ensure the `start`
// function takes ownership of the fireworks.
assert_eq!(Fireworks::start(f), "big");
assert_eq!(Fireworks::start(f), "🚀🚀🚀🚀🚀🚀🚀");
}
}