Implement "From" Trait
Learning Rust
Implement “From” trait… Basic implementation of From trait to create “MyStruct” from type: i32
struct MyStruct{ word: String, number: i32 } impl From<i32> for MyStruct{ fn from(num: i32) -> MyStruct { MyStruct { word: "notset".to_string(), number: num } } } fn main() { let m = MyStruct::from(25); let n: MyStruct = 30.into(); println!("Struct m: word={}, number={}.", m.word, m.number); println!("Struct n: word={}, number={}.", n.word, n.number); } Notice that I can run 30.
[Read More]