Maybe you ended up with a PathBuf, passed it somewhere that expected a P: AsRef<Path>[1] and lost ownership of it, and then tried to use curr_dir again afterwards. If so, pass &curr_dir instead of curr_dir. If you passed it to something that requires PathBuf, pass curr_dir.clone() instead.
If you're storing these in your own data structures, store PathBuf, not &Path.
These are just somewhat wild guesses at the errors you may be encountering.
Yes. There are also benefits to functions, operators, data structures, and traits.
They are also so fundamental to programming in Rust that it's hard to say much more.
error[E0382]: borrow of moved value: `my_dir`
--> src\main.rs:142:58
128 | let my_dir = env::current_dir()? ;
| ------ move occurs because `my_dir` has type `PathBuf`, which does not implement the `Copy` trait
129 |
130 | if let Ok(my_entries) = std::fs::read_dir( my_dir /* std::env::current_dir()? */) {
| ------ value moved here
...
142 | std::path:: Path::new( std::path:: Path::new( & my_dir/* std::env::current_dir()? */ )/* . file. . .
| ^^^^^^^^ value borrowed here after move
|
help: consider cloning the value if the performance cost is acceptable
|
130 | if let Ok(my_entries) = std::fs::read_dir( my_dir.clone() /* std::env::current_dir()? */) {
| ++++++++
In the first use case you pass the var to a function, and it gets moved there, inside, and isn't available anymore in this scope, that you show. This is fundamental feature of Rust, ownership and moving.
In this case, you can add a ref in the first use so that it stays -- the read_dir accepts anything that can become a reference to Path.
(instead of the variable) the compiler keeps silence?
So. have I to use variables?
Maybe, with variables Rust works faster or ...
This is my main question..
It's unclear which way you use read_dir() that the compiler is silent. Modify this example in Playground and post a link here, so we could understand.
If you make 2 calls to read_dir, it gets executed twice, doing two calls to the OS. With a var, you do only one call to the OS, save the result your var, and then give it to both function calls. In your case the difference in negligible, but if the call would have taken several seconds, you'd clearly notice the difference.