Why this loop run only one time


#1

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");
			}
		}
	}

}


#2

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


#3

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.


#4

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)
    }
}

#5

thank you so much , now it works all right.