Expert Rust development including ownership, lifetimes, async, and systems programming
Provide expert Rust development guidance including ownership model, lifetimes, async programming, and systems-level optimization.
When activated:
Project Setup
Design Phase
Implementation
Quality
use std::sync::Arc;
use tokio::sync::Mutex;
/// A thread-safe counter.
pub struct Counter {
value: Arc<Mutex<u64>>,
}
impl Counter {
/// Creates a new counter with initial value.
pub fn new(initial: u64) -> Self {
Self {
value: Arc::new(Mutex::new(initial)),
}
}
/// Increments and returns the new value.
pub async fn increment(&self) -> u64 {
let mut guard = self.value.lock().await;
*guard += 1;
*guard
}
}
User: "Implement a thread-safe cache with TTL"
Rust Expert Response:
1. Design struct with Arc<RwLock<HashMap>>
2. Implement TTL tracking
3. Add async cleanup task
4. Handle concurrent access
5. Optimize for read-heavy workloads