mirror of
https://git.aramjonghu.dev/AramJonghu/rustlings.git
synced 2026-09-01 16:03:31 +02:00
51 lines
1.2 KiB
Rust
51 lines
1.2 KiB
Rust
// Structs contain data, but can also have logic. In this exercise, we have
|
|
// defined the `Fireworks` struct and a couple of functions that work with it.
|
|
// Turn these free-standing functions into methods and associated functions
|
|
// to express that relationship more clearly in the code.
|
|
|
|
#![deny(clippy::use_self)] // practice using the `Self` type
|
|
|
|
#[derive(Debug)]
|
|
struct Fireworks {
|
|
rockets: usize,
|
|
}
|
|
|
|
impl Fireworks {
|
|
fn new() -> Self {
|
|
Self { rockets: 0 }
|
|
}
|
|
|
|
fn add_rockets(&mut self, rockets: usize) {
|
|
self.rockets += rockets
|
|
}
|
|
|
|
fn start(self) -> String {
|
|
"🚀".repeat(self.rockets)
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
// You can optionally experiment here.
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[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(), "🚀🚀🚀");
|
|
|
|
let mut f = Fireworks::new();
|
|
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), "🚀🚀🚀🚀🚀🚀🚀");
|
|
}
|
|
}
|