Hi,
when reading the documentation for TryFrom here it is mentioned that TryFrom<T> for U
implies TryInto
<U> for T
.
This simple example
enum Code {
Hello,
Bye,
}
impl TryFrom<u8> for Code {
type Error = String;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::Hello),
1 => Ok(Self::Bye),
_ => Err(String::from("ERROR")),
}
}
}
fn main() {
let code = Code::Bye;
let code_as_u8: u8 = match code.try_into() {
Ok(c) => c,
Err(err) => {
println!("{}", err);
return;
},
};
}
fails to compile with the following error:
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `u8: From<Code>` is not satisfied
--> src/main.rs:20:32
|
20 | let code_as_u8: u8 = match code.try_into() {
| ^^^^ -------- required by a bound introduced by this call
| |
| the trait `From<Code>` is not implemented for `u8`
|
= help: the following other types implement trait `From<T>`:
<u8 as From<NonZeroU8>>
<u8 as From<bool>>
= note: required for `Code` to implement `Into<u8>`
= note: required for `u8` to implement `TryFrom<Code>`
= note: required for `Code` to implement `TryInto<u8>`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error
Am I missing something here? When I implement TryInto manually (the docs state that it is not recommended), the code compiles without any issues.
Thanks in advance for the help!