Method not found

Hi all,

use std::fs::*;
let mut perms = metadata("Hello.txt").permissions();

When I compile this I get a ^^^^^^^^^^^ method not found in std::result::Result<Metadata, std::io::Error> on the permissions() function. However the std::fs::Metadata documentation shows that it does exist.

I am not understanding why this is so. Any help would be appreciated.

metadata() returns a Result<Permissions>, so you have to unwrap it

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


if let Ok(metadata) = metadata("Hello.txt") {
     // handle perms here
1 Like

@nologik Thanks, that worked. So if I understand correctly, unwrap() extracts the OK data from a Result<T, E>?

Yep, so long as the result was ok. Otherwise, if the result was an error and tried unwrapping it, you'd cause a panic in the thread. You almost always want to error-handle though if you want to make clean software, so rarely use unwrap by itself

1 Like

Consider also the ? operator, mentioned in this section of the Rust book.

1 Like