Working with files (Only one short function)

Seems it works. Unexpectedly started to work. I did it blindly and it was no easy.
So, now I have few Questions.
I have to create folders for files and to move that files. (Win11 does not want to do it)
Only in the current folder (no folders in folders).
Current folder is where I launch my app (app.exe is not there)

/// IMG_20220402_184432.jpg 
/// IMG_20210410_203021.jpg 
/// VID_20210410_153853.mp4 
 
fn main()-> std::io::Result<()>  {
    println!("Hello, world!");
 
     if let Ok(my_entries) = std::fs::read_dir(   std::env::current_dir()?/* my_dir */ /* "/tmp"  */ ) {
        for my_entry in my_entries {
            if let Ok(my_entry) = my_entry {
                // Here, `entry` is a `DirEntry`.
                print!("{:?}", my_entry.file_name()); 
                // let my_file =   entry.file_name().to_str().unwrap().clone()/* .to_string().as_str() */ /* .split(&"_") */     ; 
                // let mut my_dir: Vec<&str > = Vec::new();
                let my_file =   my_entry.file_name().to_str().unwrap()   .to_string()   .clone()      /* .clone_into(target) *//* .as_str() */ /* .split(&"_") */     ;
                let my_dir:Vec< &str   /* String */>= my_file   .split("_").collect /* ::<&str/* String*/ >  */ ();
    
                print!(" {:?}", my_file ); 
                println!( " {:?}", my_dir /* entry.file_name().to_str().unwrap().split("_").collect()  */   )   ;

                if my_dir.len()==3 {
                    std::fs::DirBuilder::new()
                        .recursive(true)
                        .create(  my_dir [1]   ).unwrap();  

                   std::fs::rename(&my_file , format!("{}\\{my_file}", my_dir[1]   )     )   ?   ;   
                }
                // println!( ".{:#?}", my_file /* .to_string () *//* .unwrap() */ .split("_").collect ::<String> ()./* entry.file_name().to_str().unwrap().split("_").collect()  */   )   ;
            }
        }
    }
    
   

    println!("Goodbye, world!");
    Ok(())
}

Common Q.#1:

  1. Is this approach correct ?

........................................

Lines 14, 16:

  1. How correctly to extract part of filename (for creating a folder with that name) ?

........................................

Lines 21-24:

  1. How to correctly create folder (no error, if it exists) ?

........................................

Line 26:

  1. How to compose path+filename ?

format!("{}\{my_file}" , my_dir[1] )

I think it will work only in Windows.

........................................

Common Q.#2:

  1. How about Strings, strs, &. ? I did its absolutely blindly, until compiler stops to show it's beloved red messages

........................................

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.