menu_book Course Curriculum (Rust) expand_more
Trait Implementations
Learn Trait Implementations in Rust through a focused explanation, a dedicated topic-specific example, an engineering boundary, and a mastery check.
Trait Implementations is a focused Rust lesson inside Structs Enums Traits. It teaches how Trait Implementations expresses reusable behavior while retaining compile-time type guarantees.
Read the example from the construct outward: identify the exact syntax or API named by the lesson, trace the values that enter it, and predict the result before running the code. The example is intentionally different from neighboring lessons so the behavior of Trait Implementations remains visible instead of being hidden inside a generic demo.
Topic-specific example
trait Discount {
fn discounted(&self, rate: f64) -> f64;
}
impl Discount for f64 {
fn discounted(&self, rate: f64) -> f64 {
self * (1.0 - rate)
}
}
fn main() {
println!("{:.2}", 100.0_f64.discounted(0.1));
}
For Trait Implementations, handle partial data, malformed input, unavailable resources, and cleanup; successful I/O is never the only path.