Hey hi i need help

Hello rust community I have a question can you help me?

    pub fn ReadUsernameShort() -> Result<String, io::Error> {
        let mut UsernameFile = File::open("asdad.txt")?;
        let mut Username = String::new();
        
        UsernameFile.read_to_string(&mut Username)?;

        Ok(Username)
    }

Ok() for the type "?" can we use your operator?
or this "?" Is Operator Only For Type Err()?

For example, something like:

    pub fn ReadUsernameShort() -> Result<String, io::Error> {
        let mut UsernameFile = File::open("asdad.txt")?;
        let mut Username = String::new()?;
        
        UsernameFile.read_to_string(&mut Username)?;
    }

The ? operator only works for "error" types. So the second example won't work.

1 Like

thank you i got it right

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.