FileError { kind: IOError(Os { code: 2, kind: NotFound, message: "No such file or directory" }), path: "Some(\"assets/images/tiles/air.png\")" }'

FileError { kind: IOError(Os { code: 2, kind: NotFound, message: "No such file or directory" }), path: "Some(\"assets/images/tiles/air.png\")" }'

如果我需要提供更多信息,请告诉我,我是新来的。这是代码:

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

但是我得到这个错误:

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

这真的很奇怪,因为我可以确认 'air.png' 在 'tiles' 文件夹中,我已经能够通过 load_texture() 简单地加载图像,但是不明白为什么现在加载不出来

Path::to_str returns 一个 Option<&str>。您正在对其应用调试格式 ({:?}),然后将结果添加到 image_paths。我很确定您的文件路径不应该以 Some(.

开头

当你在 Rust 中使用路径时,你通常希望使用 PathPathBuf 除非你需要 display 路径。检查 load_texture 是否可以收到 &PathPathBuf。如果没有,那么您需要 unwrap Option 或以适合您的情况的任何方式处理 None

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());