15 Prompts for Rust: Systems Programming, CLI, and WebAssembly
Rust has repeatedly been ranked as one of the most loved languages in the Stack Overflow Developer Survey. Its memory safety and performance make it a top pick for systems programming, CLI tools, and WebAssembly. Yet the borrow checker can be tough — a good AI prompt is like having a Rust expert at your side.
Below are 15 ready-to-use prompts, grouped into three areas. Each includes a prompt, a usage example, and what to expect.
Systems Programming
1. Audit unsafe code
Review this unsafe block for undefined behavior and list fixes.
Example: Pass std::slice::from_raw_parts(ptr, len); get null/alignment checks and a safe alternative. Reference: The Rustonomicon.
2. Design a safe FFI wrapper
Wrap this C function in a safe Rust API with RAII.
Example: Wrap sqlite3_open in a struct implementing Drop to free resources.
3. Explain a borrow-checker error
Why does this code fail? Show lifetimes and two fixes.
Example: Iterator holding a reference; the answer includes a Vec<&T> solution.
4. Make a type Send + Sync
Rewrite my struct to be thread-safe using Mutex.
Example: Rc<RefCell<T>> becomes Arc<Mutex<T>> with explanation.
5. Optimize a hot loop
Profile this loop and suggest iterator/SIMD optimizations.
Example: Replace for with .fold() and use std::arch intrinsics.
CLI Development
6. Generate a clap CLI
Create a clap derive struct with subcommands for a find-like tool.
Example: enum Command { Find { path: PathBuf, name: String } }.
7. Refactor errors
Use thiserror and anyhow to refactor my error enum.
Example: Outputs #[error("...")] attributes for each variant.
8. Write a nom parser
Parse an INI config file using nom, ignoring comments.
Example: Returns HashMap<String, String>, with whitespace handling.
9. Async CLI with tokio
Write a concurrent URL fetcher with timeout and retry.
Example: Uses FuturesUnordered and tokio::time::timeout.
10. Property-based tests
Add proptest tests for my URL parser.
Example: Generate random strings and assert parse_url(...).is_ok().
WebAssembly
11. Call JS callbacks
Use wasm-bindgen to accept JS closures from Rust.
Example: A Closure that calls console.log. See the wasm-bindgen guide.
12. Zero-copy byte passing
Pass a large Vec<u8> from JS to Rust without copying.
Example: Accept &[u8] and use Uint8Array::view on the JS side.
13. Optimize Wasm binary
Suggest flags to reduce this wasm size.
Example: Set opt-level = "s" and lto = true in Cargo.toml.
14. Web Worker integration
Build a Web Worker that imports wasm and posts messages.
Example: Use serde-wasm-bindgen to send structured messages.
15. build.rs for wasm32
Write a build.rs to compile C code for the wasm32 target.
Example: Emit cargo:rustc-link-lib=static=foo and cargo:rerun-if-changed.
Conclusion
Try one of these prompts in your next Rust project. The more context you provide, the more accurate the output. For further study, open the Rust Book or Rust by Example. Keep exploring, and let the compiler — and your AI assistant — make you a better Rust developer.
Comments