12 Prompts for Rust: System Programming, CLI, and WebAssembly

Rust is used by companies like Cloudflare, Microsoft, and Dropbox for performance-critical systems. Its safety guarantees (ownership, lifetimes) and a rich crate ecosystem make it ideal for CLI or WASM targets. The prompts below are templates for AI code assistants. They define the problem, scope, and constraints, so you get reliable code. Refer to The Rust Book and the Rust Reference to check generated code.

How to use these prompts

Each entry includes a problem, a prompt to copy, and a note on why it works. Replace [placeholder] with your own code or keywords.

System Programming

1. Safe FFI wrapper — Problem: call SQLite without undefined behavior.

Prompt: "You are a Rust systems expert. Write a safe wrapper for `sqlite3_prepare_v2`. Use `CString` for strings, create a `Statement` struct, implement `Drop` to finalize, and add tests."

Result: You get memory-safe code with unsafe blocks isolated at the FFI boundary.

2. Lifetime refactoring — Problem: remove .clone() in string processing.

Prompt: "Review this Rust code: [your code]. Find unnecessary clones and lifetime errors. Rewrite it with `Cow<'a, str>` and explain the borrow checker decisions."

Result: The AI explains trade-offs between borrowing and owning.

3. Arena allocator — Problem: high heap fragmentation in a game loop.

Prompt: "Implement a `GlobalAlloc` arena allocator aligned to 8 bytes. Use `#[global_allocator]`. Include tests for 1,000 allocations and frees."

Result: A custom allocator you can profile. Reference the alloc crate docs.

4. Zero-copy parser — Problem: parse a TLV protocol without buffer copies.

Prompt: "Write a nom v7 parser for a TLV format: tag (u8), length (u8), payload. Return payload as `&[u8]`. If length > remaining, return an error."

Result: Parser returns slices borrowing the input — the core of zero-copy.

CLI Tools

5. clap derive — Problem: build a search CLI with clean help texts.

Prompt: "Create a `clap` v4 derive `Parser` for `fz` (find by regex). Flags: -r, -i, -c. Arguments: directory (`PathBuf`). Write doc comments that appear in `--help`."

Result: A type-safe parser with idiomatic help.

6. Error context — Problem: add clear error messages to a file indexer.

Prompt: "Define a `thiserror` enum for a file indexer: `IoError(#[from] io::Error)` and `UnsupportedFormat(String)`. Then show a binary module using `anyhow::Context` to append file names."

Result: Library and binary errors are cleanly separated.

7. Progress bar — Problem: show progress in a long-running task.

Prompt: "Implement a terminal progress bar with only `std::io::Write` and `\r`. Use 40 segments, display percent, and skip drawing if the bar has not changed."

Result: A no-dependency progress bar that works on Unix and Windows terminals.

8. Parallel search — Problem: speed up recursive grep.

Prompt: "Write a Rust CLI using `walkdir` and `rayon` to search files with a regex. Support `--ignore-case`. Print `file:line:content` for matches."

Result: A strategy similar to ripgrep. Use par_iter to scan files in parallel.

WebAssembly

9. wasm-bindgen export — Problem: expose a Rust struct to JavaScript.

Prompt: "Show how to export a `Counter` struct with `wasm_bindgen`: methods `increment`, `get`, `reset`. Also serialize a `Vec<Point>` with `serde-wasm-bindgen`. Include Cargo.toml and JS call."

Result: Generated TypeScript definitions become your API contract.

10. Binary size — Problem: keep wasm under 1 MB.

Prompt: "Give a Cargo release profile for wasm32 target: `lto = true`, `panic = \"abort\"`, `opt-level = \"z\"`. Recommend the `twiggy` tool for size analysis. List common dependencies that bloat code."

Result: A smaller module fits into browser budget.

11. Runtime plugin — Problem: load untrusted WASM modules.

Prompt: "Write a `wasmtime` host program that loads `module.wasm` and calls `add(i32, i32) -> i32`. Print the result. Configure a memory limit."

Result: A sandboxed plugin host; see wasmtime.dev for safety docs.

12. DOM update — Problem: modify the DOM from Rust.

Prompt: "Provide a `web-sys` function that gets an element by ID, changes `inner_html`, and adds a click event listener. List the required Cargo.toml features for `web-sys`."

Result: Browser DOM access in Rust. Visit docs.rs/web-sys for feature flags.

Conclusion

These prompts turn generic AI suggestions into reliable engineering artifacts. Start with #1 or #5, verify the output against the official docs, and then adapt the pattern to your own tasks. If you have a favorite prompt, share it on the asibiont blog.

← All posts

Comments