cognitive-engineering-lab / rust-book

The Rust Programming Language: Experimental Edition
https://rust-book.cs.brown.edu
Other
504 stars 82 forks source link

Question: testing read permission of a path #98

Closed musjj closed 1 year ago

musjj commented 1 year ago

I was reading your version of The Rust Book, and I find the read/write/own permission concept pretty interesting. But I have a question, and since I don't know where else to ask about this, I'm posting this here. How do you test if a path is readable?

For example, you can test if a path has own and write permissions like this:

let mut s = String::from("hello world");
s = String::from("changed"); // `s` has write permission
let s_1 = s; // `s` has ownership permission

But how do you test if a path has read permission?

willcrichton commented 1 year ago

The simplest test of whether you have read permissions on a path p is that you can construct an immutable reference &p. For example, if you have a string s, then you have read permissions if you can call s.len() which desugars to str::len(&s).

This is a good question though-- I'll think about how we can make this clearer in the book

musjj commented 1 year ago

Thanks, that clarifies my doubt!