Closed
Description
Problem
I had an error in my code where I tried to convert a u32
to an enum using try_into
without implementing it first. My code essentially looked like this:
enum Thing {
First,
Second,
}
struct BigThing {
value: u32,
other: u16,
thing: Thing,
}
impl BigThing {
fn new(value: u32, other: u16, thing: Thing) -> Self {
BigThing {value, other, thing}
}
}
//-------------------------------------------VVV note that this is u32, not Thing
fn make_thing(value: u32, other: u16, thing: u32) -> BigThing {
BigThing::new(value, other, thing.try_into().unwrap())
}
This code generates the following error message:
error[E0277]: the trait bound `Thing: From<u32>` is not satisfied
--> src/starlet/iso/iso.rs:67:19
|
67 | BigThing::new(value, other, thing.try_into().unwrap())
| ------------- ^^^^^ the trait `From<u32>` is not implemented for `Thing`
| |
| required by a bound introduced by this call
|
= note: required because of the requirements on the impl of `std::convert::Into<Thing>` for `u32`
= note: required because of the requirements on the impl of `TryFrom<u32>` for `Thing`
= note: required because of the requirements on the impl of `TryInto<Thing>` for `u32`
The compiler correctly notes that I have not implemented TryInto
on Thing
for u32
, but it tells me that I haven't satisfied that trait bound for value
, not thing
. This problem does not occur if I use a struct initializer, only if I use a function.
Steps
- Copy the above code.
- Run
cargo check
Possible Solution(s)
My guess here is that when showing an error for trait bound in a function argument, the checker defaults to the first argument. I'm not familiar with cargo
code though so that's just speculation.
Notes
I am using the nightly compiler, but the same problem occurs on stable.
Version
cargo 1.64.0-nightly (a5e08c470 2022-06-23)
release: 1.64.0-nightly
commit-hash: a5e08c4703f202e30cdaf80ca3e7c00baa59c496
commit-date: 2022-06-23
host: x86_64-unknown-linux-gnu
libgit2: 1.4.2 (sys:0.14.2 vendored)
libcurl: 7.83.1-DEV (sys:0.4.55+curl-7.83.1 vendored ssl:OpenSSL/1.1.1n)
os: Arch Linux Rolling Release [64-bit]