# Prompt: Guiding Principles for Rust Code Generation
When generating Rust code for me, please adhere to the following core principles and design guidelines. My primary focus is on building robust, performant, and maintainable native applications (desktop or server-side).
Core Principles:
- Safety First: Fully leverage Rust's ownership, borrowing, lifetimes, and type system to guarantee memory safety and prevent data races. Explain how these features are used to achieve safety in the generated code.
- Zero-Cost Abstractions: Employ traits, generics, and other language features to create high-level, reusable abstractions without introducing runtime overhead.
- Explicitness and Clarity: Prioritize code that is clear, explicit, and easy to reason about. Avoid unnecessary complexity or "magic" where straightforward code suffices.
- Composition Over Inheritance: Design solutions using traits and struct composition as the primary means of code reuse and polymorphism.
- Robust Error Handling: Utilize `Result<T, E>` consistently for operations that can fail. Define meaningful, specific error types where appropriate. Reserve `panic!` for unrecoverable program states or broken invariants.
- Performance Awareness: Generate code that is efficient by default. Be mindful of algorithmic complexity, data structures, memory allocation patterns (stack vs. heap), and I/O operations. Highlight potential performance trade-offs if relevant.
Design Guidelines:
- Modularity: Structure code into well-defined modules with clear responsibilities and minimal coupling.
- API Design: Aim for ergonomic, intuitive, and well-documented public APIs (functions, structs, traits, modules).
- Testability: Write code in a way that facilitates unit and integration testing.
- Dependency Management: Prefer solutions using the Rust standard library whenever practical. If external crates are necessary, select well-maintained, widely-used options and briefly justify their inclusion.
- Concurrency: When concurrency is required, use Rust's safe concurrency primitives (`Arc`, `Mutex`, channels, `async/await`) correctly.
Output Expectations:
- Idiomatic Code: Strictly follow `rustfmt` formatting and established community best practices.
- Context and Explanation: Provide necessary `use` statements, type definitions, and concise explanations for key design choices, trade-offs, or Rust-specific concepts involved.