Home > OS >  FileError { kind: IOError(Os { code: 2, kind: NotFound, message: "No such file or directory&quo
FileError { kind: IOError(Os { code: 2, kind: NotFound, message: "No such file or directory&quo

Time:03-30

Tell me if I need to provide more info, I am rather new here. Here is the code:

let mut image_paths = vec![];

let mut images = vec![];

let mut image_path = "assets/images/tiles/";

for img in glob(format!("{}*.png",image_path).as_str()).unwrap() {
    match img {
        Ok(path) =>{ 
            image_paths.push(format!("{:?}",path.to_str()));
        },
        Err(e) => continue,
    }
}

for i in &image_paths {
    images.push(load_texture(i).await.unwrap());
}

But I get this error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: FileError { 
kind: IOError(Os { code: 2, kind: NotFound, message: "No such file or directory" }), 
path: "Some(\"assets/images/tiles/air.png\")" }', src/main.rs:71:43
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Which is really bizarre, because I can confirm that 'air.png' is in the 'tiles' folder, I have been able to load the image simply by doing load_texture(), but I don't understand why I cannot load it now.

CodePudding user response:

Path::to_str returns an Option<&str>. You're applying debug formatting ({:?}) on that, then adding the result to image_paths. I'm pretty sure your file path isn't supposed to start with Some(.

When you work with paths in Rust, you generally want to use Path or PathBuf unless you need to display the path. Check to see if load_texture could receive a &Path or a PathBuf. If not, then you need to unwrap the Option or deal with the None in whatever way is appropriate for your situation.

image_paths.push(path.to_str().unwrap());

// or if you need a `String` instead of a `&str`:
image_paths.push(path.to_str().unwrap().to_string());
  • Related