Rust 如何在库中包含图像文件

Rust 如何在库中包含图像文件,rust,Rust,我正在创建一个Rust库,其中的结构表示单个Hanafuda扑克牌和整个扑克牌组。我正计划用同一副牌制作几个不同的游戏,所以我更喜欢把它放在一个库中,而不是硬编码到每个项目中。每张卡片都有一张相关联的PNG人脸图像 我遇到的问题是,一旦库作为依赖项包含在另一个项目中,如何访问这些图像。我更喜欢将图像打包为普通的旧图像文件,因为我还不确定是否要使用gtk rs或rust qt或其他GUI库构建游戏 以下是扑克牌的结构和新功能: pub struct HanafudaCard { pub m

我正在创建一个Rust库,其中的结构表示单个Hanafuda扑克牌和整个扑克牌组。我正计划用同一副牌制作几个不同的游戏,所以我更喜欢把它放在一个库中,而不是硬编码到每个项目中。每张卡片都有一张相关联的PNG人脸图像

我遇到的问题是,一旦库作为依赖项包含在另一个项目中,如何访问这些图像。我更喜欢将图像打包为普通的旧图像文件,因为我还不确定是否要使用gtk rs或rust qt或其他GUI库构建游戏

以下是扑克牌的结构和新功能:

pub struct HanafudaCard {
    pub month: Month,
    pub plant: Plant,
    pub face: String,
    pub card_type: CardType,
    pub value: u8,
    pub image_path: String
}

impl HanafudaCard {
    pub fn new(month: Month, plant: Plant, face: String, card_type: CardType, value: u8, image_path: String) -> Self {
        HanafudaCard {
            month,
            plant,
            face,
            card_type,
            value,
            image_path
        }
    }
}
我使用
build.rs
脚本将映像目录复制到目标构建中:

extern crate fs_extra;
use std::env;
use fs_extra::dir::{copy, CopyOptions};

fn main() {
  println!("cargo:rerun-if-changed=build.rs");

  let out_dir = env::var("OUT_DIR").unwrap();
  copy("./res/img", &out_dir, &CopyOptions::new()).unwrap();
}
OUT\u DIR
在本例中,当我在库目录中运行
cargo build
时,指向
target/debug/build/hanafuda\u deck\rs-0327e50b2deefc9f/OUT/img/01.png

hanafudeck::new
中,将单个卡映像分配给
HanafudaCard
结构,如下所示:

impl HanafudaDeck {
    pub fn new() -> Self {
        HanafudaDeck {
            cards: vec![
                HanafudaCard::new(Month::January, Plant::Pine, String::from("Crane and Sun"), CardType::Bright, 20, String::from(format!("{}/img/01.png", env::var("OUT_DIR").unwrap()))),
                // other 47 card definitions here
            ]
        }
    }
}
现在,我正在通过Git导入板条箱。当将此库导入到另一个项目中时,图像文件在运行
货物构建时会以ex.
/target/debug/build/hanafuda_deck\rs-edd17a51cf1d380c/out/img/01.png
结束

但是当我运行
货物运行
时,我会感到以下恐慌:

Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/hanafuda-grid`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: NotPresent', /home/shane/.cargo/git/checkouts/hanafuda-deck-rs-ccc798d3bc429f2a/3909aa2/src/lib.rs:87:175
为什么
OUT\u DIR
最终指向该目录而不是
/target
中的目录?我应该做些不同的事情来匹配路径吗