Crate include_dir [−] [src]
The logical evolution of the include_str!()
macro to allow embedding
entire file trees.
If you want to use this in stable, you'll need a build.rs
build script.
extern crate include_dir; use std::env; use std::path::Path; use include_dir::include_dir; fn main() { let outdir = env::var("OUT_DIR").unwrap(); let dest_path = Path::new(&outdir).join("assets.rs"); include_dir("assets") .as_variable("SRC") .to_file(dest_path) .unwrap(); }
Then in one of your source files, you'll need to include the generated
assets.rs
file. Notice that it's been included as a submodule, that
way the code include_dir
generates won't pollute the rest of your project.
By making it public, you also gain the ability to check out the generated
assets and see what you can do with them.
#[allow(dead_code)] pub mod assets { include!(concat!(env!("OUT_DIR"), "/assets.rs")); }
Note
Because a large part of this crate's functionality comes from code generation, the best reference for features and examples will be the integration tests directory.
Features
By default include_dir
requires no extra runtime dependencies. This is
great because you just need to run the build script and include the
generated file and then have full access access to the embedded assets,
however it does limit your ability to access some nice features.
Glob Search
This feature adds the ability to search for files using glob patterns. It
requires you to add extern crate glob
to the top of your lib.rs
or '
main.rs
, and is accessible using the globs
feature flag.
extern crate glob; for entry in ASSETS.glob("*.rs")? { println!("{}", entry.path().display()); }
Modules
errors |
Submodule containing code generated by |
Structs
IncludeDirBuilder |
A builder object to give the |
Functions
include_dir |
The entire purpose of this crate. |