[][src]Function tao_of_rust::ch03::type_cast::from_into

pub fn from_into()

类型转换:From和Into

Base usage: String实现了From

let string = "hello".to_string();
let other_string = String::from("hello");
assert_eq!(string, other_string);Run

Base usage: 使用into简化代码

#[derive(Debug)]
struct Person{ name: String }
impl Person {
    fn new<T: Into<String>>(name: T) -> Person {
        Person {name: name.into()}
    }
}
let person = Person::new("Alex");
let person = Person::new("Alex".to_string());
println!("{:?}", person);Run