Skip to content

Error Code Samples in Docs #36343

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

Closed
kosinix opened this issue Sep 8, 2016 · 2 comments
Closed

Error Code Samples in Docs #36343

kosinix opened this issue Sep 8, 2016 · 2 comments

Comments

@kosinix
Copy link

kosinix commented Sep 8, 2016

Example from here: https://doc.rust-lang.org/std/fs/struct.File.html

use std::io::prelude::*;
use std::fs::File;

let mut f = try!(File::create("foo.txt"));
try!(f.write_all(b"Hello, world!"));

let mut f = try!(File::open("foo.txt"));
let mut s = String::new();
try!(f.read_to_string(&mut s));
assert_eq!(s, "Hello, world!");

Compiling results to: error: mismatched types [E0308]
Funny because when running rustc --explain E0308 it tells me not to use try! with File:

rustc --explain E0308:

Another situation in which this occurs is when you attempt to use the `try!`                                               
macro inside a function that does not return a `Result<T, E>`:   

use std::fs::File;                                                                                                         

fn main() {                                                                                                                
    let mut f = try!(File::create("foo.txt"));                                                                             
}                                                                                                                          
@steveklabnik
Copy link
Member

Funny because when running rustc --explain E0308 it tells me not to use try! with File:

This isn't what it's telling you: it's telling you that you can't use try! from within a function that doesn't return a Result, like main().

@steveklabnik
Copy link
Member

Closing as "not a bug", thanks!

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

3 participants