Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- struct MotorImpl {
- speed: u8,
- }
- #[derive(Debug)]
- struct SpecializedMotorImpl(MotorImpl);
- impl MotorImpl {
- fn new() -> MotorImpl {
- MotorImpl { speed: 0 }
- }
- }
- trait Motor {
- fn run(&mut self) {
- self.set_speed(1);
- }
- fn stop(&mut self) {
- self.set_speed(0);
- }
- fn set_speed(&mut self, speed: u8);
- }
- impl Motor for MotorImpl {
- fn set_speed(&mut self, speed: u8) {
- self.speed = speed
- }
- }
- trait SpecializedMotor: Motor {
- fn run_fast(&mut self) {
- self.set_speed(2);
- }
- }
- impl Motor for SpecializedMotorImpl {
- fn set_speed(&mut self, speed: u8) {
- self.0.set_speed(speed)
- }
- }
- impl SpecializedMotor for SpecializedMotorImpl {}
- fn main() {
- let mut motor = MotorImpl::new();
- let mut specialized_motor = SpecializedMotorImpl(MotorImpl::new());
- println!("{:?}", motor);
- motor.run();
- println!("{:?}", motor);
- motor.stop();
- println!("{:?}", motor);
- println!("{:?}", specialized_motor);
- specialized_motor.run();
- println!("{:?}", specialized_motor);
- specialized_motor.stop();
- println!("{:?}", specialized_motor);
- specialized_motor.run_fast();
- println!("{:?}", specialized_motor);
- specialized_motor.stop();
- println!("{:?}", specialized_motor);
- }
Add Comment
Please, Sign In to add comment