Structs and enums
โ Report an issue with this lessonstruct Course {
title: String,
price: f64,
}
enum Level {
Beginner,
Intermediate,
Advanced,
}
impl Course {
fn describe(&self) -> String {
format!("{} -- ${}", self.title, self.price)
}
}
Rust's enums are more powerful than most languages' -- each variant can hold its own data, making them a natural fit for modeling things like API responses (success vs. error, each with different attached data).
Try it yourself
Exercise: Define a Level enum with Beginner/Intermediate/Advanced and match on one to print a message.
Expected output: open-ended โ there's no single correct output here, just get your code running without errors.
Run your code and get it working before marking this lesson complete.
// that was the last free lesson
9 more lessons โ including Capstone project: a course catalog โ plus a certificate are waiting.
Unlock the full course โ $149.99