rust
9 lines · 1 tab
Marcus Chen
Jan 2026
1 tab
// build.rs
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let version = env!("CARGO_PKG_VERSION");
println!("cargo:rustc-env=BUILD_VERSION={}", version);
// Could generate code, compile C, etc.
}
1 file · rust
Explain with highlit
A build.rs file runs before compiling your crate, enabling code generation, FFI binding generation, or environment checks. I use build scripts to generate Rust code from proto files (with prost), compile C libraries, or set cfg flags based on the target platform. The script communicates with cargo via println!("cargo:...") directives. For example, println!("cargo:rerun-if-changed=proto") tells cargo when to re-run. Build scripts have access to the crate's manifest and can depend on build-time-only crates. They're powerful but add complexity, so I use them judiciously. For code gen, they're often the cleanest solution.
Related snips
go
package api
import (
"net/http"
"runtime/debug"
)
Expose build metadata for debugging deploys
go
observability
build
by Leah Thompson
1 tab
rust
struct Config<'a> {
name: &'a str,
value: &'a str,
}
fn parse_config(line: &str) -> Config {
Lifetime annotations for flexible borrowing in structs
rust
lifetimes
borrowing
by Marcus Chen
1 tab
rust
use crossbeam::channel::unbounded;
use std::thread;
fn main() {
let (tx, rx) = unbounded();
Crossbeam for advanced concurrent data structures
rust
concurrency
lock-free
by Marcus Chen
1 tab
rust
use tracing::{info, instrument};
#[instrument]
fn process_request(user_id: u64) {
info!(user_id, "Processing request");
// Work happens here
tracing for structured logging and distributed tracing
rust
observability
tracing
by Marcus Chen
1 tab
rust
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
Channels (mpsc) for message passing between threads
rust
concurrency
channels
by Marcus Chen
1 tab
rust
use clap::Parser;
#[derive(Parser, Debug)]
#[command(author, version, about)]
struct Args {
#[arg(short, long)]
clap for CLI argument parsing with derive macros
rust
cli
clap
by Marcus Chen
1 tab
Share this code
Here's the card — post it anywhere.