4
use std::convert::TryFrom;

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

The compiler complains with

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`

What should I do?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Cheng-Chang Wu
  • 187
  • 1
  • 7
  • 1
    This error existed on a previous Rust version, where TryFrom required From to be implemented. Currently it works -- see: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e180606f46282e914f323a1d6c23179f – zertyz Oct 14 '21 at 22:04

0 Answers0