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

std::fs::File functions panic on valid return type #49491

Closed
jhpratt opened this issue Mar 30, 2018 · 4 comments
Closed

std::fs::File functions panic on valid return type #49491

jhpratt opened this issue Mar 30, 2018 · 4 comments

Comments

@jhpratt
Copy link
Member

jhpratt commented Mar 30, 2018

The following code taken directly from documentation will not compile.

fn main() {
    use std::fs::File;
    use std::io::prelude::*;

    let mut file = File::create("foo.txt")?;
    file.write_all(b"Hello, world!")?;
}

The error message is as follows:

error[E0277]: the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
 --> example.rs:5:20
  |
5 |     let mut file = File::create("foo.txt")?;
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
  |
  = help: the trait `std::ops::Try` is not implemented for `()`
  = note: required by `std::ops::Try::from_error`

error[E0277]: the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
 --> example.rs:6:5
  |
6 |     file.write_all(b"Hello, world!")?;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
  |
  = help: the trait `std::ops::Try` is not implemented for `()`
  = note: required by `std::ops::Try::from_error`

error: aborting due to 2 previous errors

The documentation for create() indicates that the function does return Result<File>, but the compiler believes it returns (). write_all() indicates it returns Result<()>.

Clearly, both functions implement std::ops::Try via Result, but an example as basic as this fails on that test.

Version

Fails on both stable and nightly.

rustc --version
rustc 1.25.0 (84203cac6 2018-03-25)
rustc 1.26.0-nightly (e5277c145 2018-03-28)

@mattico
Copy link
Contributor

mattico commented Mar 30, 2018

Notice the error message:

cannot use the ? operator in a function that returns ()

main is a function that returns (), so you can't use the ? operator inside of it.

This restriction is changing soon, but in the mean time I'd suggest using a separate function:

use std::fs::File;
use std::io::{self, prelude::*};

fn run() -> io::Result<File> {
    let mut file = File::create("foo.txt")?;
    file.write_all(b"Hello, world!")?;
    
    Ok(file)
}

fn main() {
    run().unwrap();
}

@jhpratt
Copy link
Member Author

jhpratt commented Mar 30, 2018

Ah, ok. English can be so finicky — a single word changes everything.

I certainly look forward to the issue you've linked to.

Thanks!

@jhpratt jhpratt closed this as completed Mar 30, 2018
@mattico
Copy link
Contributor

mattico commented Mar 30, 2018

Yeah, I was confused when I read the error message too - and I've been speaking English for a while...

@jhpratt
Copy link
Member Author

jhpratt commented Mar 30, 2018

Oh, I'm a native speaker. That's probably why I missed it.

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

2 participants