Exerpad
🔥0
0 XP
Lv.1 Beginner
Log In

Vehicle Trait

Define a trait Vehicle with two methods: name(&self) -> &str and speed(&self) -> i32. Implement it for Car (name: "Car", speed: 120) and Bike (name: "Bike", speed: 25). Write a function race_info that takes &impl Vehicle and prints the vehicle info.

Expected output:

text
Car goes 120 km/h
Bike goes 25 km/h

Hints:

  • Use unit structs: struct Car; and struct Bike;
  • fn race_info(v: &impl Vehicle) prints using v.name() and v.speed()
rust
fn main() {
}