#Need guidance with how to work with ownership ,_,

6 messages · Page 1 of 1 (latest)

errant mason
#
 println!("\nFiles in upload folder:\n");
            let mut dir_list:ReadDir = fs::read_dir("./").unwrap();



            for entry in dir_list {

                println!("- {}", entry.unwrap().file_name().into_string().unwrap())
            }

            if dir_list.count() == 0{
                println!("Uploads folder is empty put something u wanna upload in it!")
            }

This code gives an error that dir_list is moved when it's used in the for loop, and I thought I could just reference it by calling the .by_ref() function but that ended up removing all of the elements of the array in the for loop.

What I am asking for is some explanation why this happens?

worthy tide
#

Iterators are not collections of elements; they are sources of elements.

#

When you use a for loop or .count(), you are taking elements from the iterator; it no longer contains them.

errant mason
#

ohh okai

worthy tide
#
let mut empty = true;
for entry in dir_list {
    let entry = entry.unwrap();
    empty = false;
    println!("- {}", entry.file_name().into_string().unwrap());
}

if empty {
    println!("Uploads folder is empty put something u wanna upload in it!");
}
#

this way you check emptiness based on something you know anyway: whether the loop iterated or not