Using .into() when type inference is impossible

前端 未结 4 1314
情话喂你
情话喂你 2020-12-03 17:08

I\'m hoping to be able to use .into() to convert a value in a context where type inference is impossible. This is typically when I want to convert a temporary v

4条回答
  •  既然无缘
    2020-12-03 17:35

    You could use From::from:

    use std::convert::*;
    
    struct NewType(pub i32);
    
    impl From for i32 {
        fn from(src: NewType) -> i32 {
            src.0
        }
    }
    
    fn main() {
        let a = NewType(5);
        println!("{}", i32::from(a));
    }
    

    You can read more about it in the docs for the convert module.

提交回复
热议问题