hs-rust-learn 0.1.0

hs's rust test learn
Documentation
use std::fmt::Debug;



pub trait Summary {
    fn summarize_author(&self) -> String;
    fn summarize(&self) -> String;
}

impl Summary for String {
    fn summarize(&self) -> String {
        unsafe {
            format!("{}", self.get_unchecked(0..self.len() / 2))
        }
    }
    fn summarize_author(&self) -> String {
        format!("(author) {}", self.summarize())
    }
}

pub trait TrafficTool {
    fn get_speed(&self) -> u32;
    fn get_name(&self) -> String;
    fn move_(&self, speed: u32) -> String;
}

pub struct Car {
    pub name: String,
    pub speed: u32,
}

impl TrafficTool for Car {
    fn get_speed(&self) -> u32 {
        self.speed
    }
    fn get_name(&self) -> String {
        self.name.clone()
    }
    fn move_(&self, speed: u32) -> String {
        format!("{} moves at {} km/h", self.name, speed)
    }
}

pub struct Train {
    pub name: String,
    pub speed: u32,
}

impl TrafficTool for Train {
    fn get_speed(&self) -> u32 {
        self.speed
    }
    fn get_name(&self) -> String {
        self.name.clone()
    }
    fn move_(&self, speed: u32) -> String {
        format!("{} moves at {} km/h", self.name, speed)
    }
}

pub fn notify<T: TrafficTool>(traffic_tool: T) {
    println!("{}", traffic_tool.move_(traffic_tool.get_speed()));
}

pub fn notify2<T, U>(traffic_tool: T, speed: U) -> impl Summary
where
    T: TrafficTool + Clone,
    U: Into<u32> + Debug,
{
    let s = traffic_tool.move_(speed.into());
    println!("{}", s);
    s
}

fn main() {
    let s = String::from("hello world");
    println!("{}", s.summarize());

    let c = Car {
        name: String::from("lexus"),
        speed: 120,
    };
    notify(c);
    let t = Train {
        name: String::from("G10"),
        speed: 260,
    };
    notify(t);
}