Skip to content

Error Code Samples in Docs #36343

Closed
Closed
@kosinix

Description

@kosinix

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"));                                                                             
}                                                                                                                          

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions