Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TryFrom/TryInto 第四题 答案报错, 答案有问题 #485

Open
LukerSpringtree opened this issue Dec 22, 2023 · 0 comments
Open

TryFrom/TryInto 第四题 答案报错, 答案有问题 #485

LukerSpringtree opened this issue Dec 22, 2023 · 0 comments

Comments

@LukerSpringtree
Copy link

https://zh.practice.rs/type-conversions/from-into.html#tryfromtryinto

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`.

rustc 1.74.0 (79e9716c9 2023-11-13)

@LukerSpringtree LukerSpringtree changed the title TryFrom/TryInto 第四题 答案都报错 TryFrom/TryInto 第四题 答案报错, 答案有问题 Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant