Cannot compile example code

Hello Folks,
I am trying to teach myself file handling and am working from the book as well as the documentation. I have some code that will not compile. I can't see why not. Specifically line 8. The compiler complains it cannot find the method permissions(). As well as the code I have included the error and documentation for fs::Metadata which lists the permissions() function. Any help would be appreciated. Thanks

use std::fs::File;
use std::fs::metadata;
use std::io::Write;
use std::fs;

fn main() {
    let f = File::open("hello.txt");
    let mut perms = metadata("hello.txt").permissions().unwrap();
    perms.set_readonly(false);
    fs::set_permissions("hello.txt", perms);
    
    let mut f = match f {
        Ok(file) => file,
        Err(error) => panic!("Problem opening the file: {:?}", error),
    };
    let bytes = f.write(b"A line in the file").unwrap();
    f.flush();
    println!("Bytes written: {}", bytes);
}

you need to unwrap() before to extract the type of the Result<>:

let mut perms = metadata("hello.txt").unwrap().permissions();

@elsuizo. Perfect. Thank you

1 Like