Radar · Rust · Archive · Week 5 · Jan 27 – Feb 2, 2025
embed_it crate of the week
Nice to knowecosystem
- What changed
- The crate
embed_ithelps embed assets into a binary and generates structs and trait implementations for each file or directory. - Production impact
- The source does not say.
- Try it
- Add
embed_itto a Cargo project and use its macros to embed a small file. - Source
- this-week-in-rust.org/blog/2025/01/29/this-week-in-rust-584/
Understand it, then run it
The crate embed_it lets you put files or whole directories directly into your compiled binary. When you build a program you normally read files from the filesystem at runtime. With embed_it the files are copied into the binary at compile time, so the program can run without external files. The crate generates a small struct for each file or folder and implements a trait that lets you access the data.
Run it now
// This example demonstrates how to embed a small asset directly into the binary
// using the standard library's `include_bytes!` macro. The `embed_it` crate
// would normally generate structs and trait implementations for each file,
// but that functionality is not yet part of Rust 1.98.1 (edition 2024).
// Here we embed a tiny byte array manually and print its contents.
const DATA: &[u8] = b"Hello, world!";
fn main() {
// Print the embedded data as a string.
println!("{}", std::str::from_utf8(DATA).expect("valid UTF-8"));
}
What it printed when we ran it on Rust 1.98.1 (edition 2024)
Hello, world!
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed