Hello,
first of all, I am pretty new in Rust (started yesterday) but I am developing software for over 35 Years now in many different languages. I really like the idea of Rust to go back to the roots and so I like to learn it (even if I will never be paid for it, because I am in a good position which hopefully will last till I retire
As my first try after reading the first 10 chapters of the manual I wanted to read all files in a folder recursivly. After a few hours of try and error I managed it and the program works. But dont undertand one thing:
Here is the working code: (Please forgive me the wrong bracket format but I use this so many years I am not able to change it to this new "first brace in same line" format without getting headache)
fn main()
{
check_files("/Users/xxx/css");
}
fn check_files(path : &str)
{
if let Ok(entries) = read_dir(path)
{
for entry in entries
{
if let Ok(direntry) = entry
{
if let Ok(metadata) = direntry.metadata()
{
if metadata.is_dir()
{
let pathname = direntry.path();
let pathname = pathname.to_str();
match pathname
{
Some(name) =>
{
let pathname = name;
check_files(pathname);
},
None => {}
}
}
else
{
let filename = direntry.file_name();
let filename = filename.to_str();
println!("{:?}", filename);
}
}
}
}
}
}
What takes me a while is to find out, that i have to assign direntry.path() to a local variable to make it not Temporary. Especially that I can overwrite it in the next line with the value I really need i the thing I dont understand. If I change:
let pathname = direntry.path();
let pathname = pathname.to_str();
match pathname...
to
let pathname = direntry.path().to_str();
match pathname
I get an error. In my Eyes this makes absolutly no sense. Can anyone please explain it to me?
Thanks
Claus
P.S. How the hell can I insert formatted code in a post?