为什么`i32`没有实现特性`std :: convert :: From <isize>`?

时间:2018-03-31 19:06:21

标签: rust type-conversion

use std::convert::TryFrom;

fn main() {
    let a: isize = 1;
    let b: i32 = match TryFrom::<isize>::try_from(a) {
        Ok(x) => x,
        Err(_) => 0,
    };
}

编译器抱怨

error[E0277]: the trait bound `i32: std::convert::From<isize>` is not satisfied
 --> src/main.rs:5:24
  |
5 |     let b: i32 = match TryFrom::<isize>::try_from(a) {
  |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<isize>` is not implemented for `i32`
  |
  = help: the following implementations were found:
            <i32 as std::convert::From<i8>>
            <i32 as std::convert::From<i16>>
            <i32 as std::convert::From<u16>>
            <i32 as std::convert::From<u8>>
  = note: required because of the requirements on the impl of `std::convert::TryFrom<isize>` for `i32`

我该怎么办?

0 个答案:

没有答案