fn main() {
let n: i16 = 256;
let n: u8 = match n.try_into() {
Ok(n) => n,
Err(e) => {
println!("there is an error when converting: {:?}, but we catch it", e.to_string());
0
}
};
assert_eq!(n, 0);
println!("Success!")
}
报错如下:
error[E0599]: no method named `try_into` found for type `i16` in the current scope
--> main.rs:4:25
|
4 | let n: u8 = match n.try_into() {
| ^^^^^^^^ method not found in `i16`
|
::: C:\Users\lde1wx\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\convert\mod.rs:569:8
|
569 | fn try_into(self) -> Result<T, Self::Error>;
| -------- the method is available for `i16` here
|
= help: items from traits can only be used if the trait is in scope
= note: 'std::convert::TryInto' is included in the prelude starting in Edition 2021
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
1 + use std::convert::TryInto;
|
error: aborting due to previous error
For more information about this error, try `rustc --explain E0599`.
https://zh.practice.rs/type-conversions/from-into.html#tryfromtryinto
报错如下:
rustc 1.74.0 (79e9716c9 2023-11-13)