Why this loop run only one time

use std::fs;

pub fn test() {

	/*
	let mut entry = fs::read_dir("."); 	
	match entry {
			Some(Ok(dir_enty)) => {
				println!("{:?}", dir_enty.path());
			}
			_ => {
				println!("{:?}", "i dont know");
			}
		}
}
*/

	for mut entry in fs::read_dir(".") {
		match entry.next() {
			Some(Ok(dir_entry)) => {
				println!("{:?}", dir_entry.path());
				continue;
			}
			Some(Err(err)) => {
				println!("{:?}", err);
			}
			None => {
				println!("{:?}", "none result");
			}
		}
	}

}

1 Like

What are you expecting? e.g. what's the contents of .?

there are more than one file in the current directory, i want to list them all, and this function just print the first of it.

fs::read_dir returns a Result, so you need to either handle it somehow or unwrap it.

for entry in fs::read_dir(".").unwrap() {
    match entry {
        Ok(dir_entry) => println!("{:?}", dir_entry.path()),
        Err(err) => println!("{:?}", err)
    }
}
1 Like

thank you so much , now it works all right.