Systems Programming Best Practices
Rules for safe, portable, well-tested systems and FFI code in Rust.
Busca en todas las páginas de la documentación
Rules for safe, portable, well-tested systems and FFI code in Rust.
unsafe as a security-sensitive changeunsafe scope. Keep FFI in dedicated modules with reviews.Drop.# Safety preconditions. Every unsafe fn lists caller obligations.repr(C) for exported structs. Never assume Rust default layout crosses FFI.size_of/align_of against C headers.cfg drift early.Path, not strings. Avoid hardcoded / separators.cfg(unix) / cfg(windows) modules.wait after spawn.spawn_blocking for syscall-heavy work... traversal in privileged tools.If safe wrappers cannot hide it, split into smaller reviewed functions with clear invariants.
When the C API has more than a handful of functions or changes frequently.
Prefer std, then nix, then raw libc as you need lower-level control.
Never block the runtime. Offload to blocking threads or use async-native I/O.
Track CVEs, enable ASan in tests, and sandbox privileged operations.
Only commit to ABI stability with repr(C), semver, and explicit compatibility tests.
Set atomic flags only. Defer real work to the main thread or runtime.
Set explicit modes on created files. Do not rely on umask defaults for secrets.
Share core logic in a library; thin binaries for service and CLI entrypoints.
Vendor known hashes for downloaded SDKs. Verify checksums in build.rs.
cfg patternsStack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Revisado por Chris St. John·Última actualización: 16 jul 2026