Generation

Walked App

Here's a simple implementation of a Walked App in Rust. This app will allow users to create a new walk, view existing walks, and delete walks. **walked_app.rs** ```rust use std::collections::HashMap; use std::fmt; // Define a Walk struct to hold walk data #[derive(Debug)] struct Walk { id: i32, name: String, distance: f64, duration: f64, } // Implement fmt::Display for Walk to print walk data impl fmt::Display for Walk { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Walk {}: {} ({:.2} km, {:.2} hours)", self.id, self.name, self.distance, self.duration) } } // Define a WalkedApp struct to hold walk data and methods struct WalkedApp { walks: HashMap<i32, Walk>, } impl WalkedApp { // Create a new WalkedApp instance fn new() -> Self { WalkedApp { walks: HashMap::new(), } } // Create a new walk and add it to the app fn create_walk(&mut self, name: String, distance: f64, duration: f64) -> i32 { let id = self.walks.len() as i32 + 1; self.walks.insert(id, Walk { id, name, distance, duration }); id } // View all walks in the app fn view_walks(&self) { println!("Walks:"); for walk in self.walks.values() { println!("{}", walk); } } // Delete a walk by ID fn delete_walk(&mut self, id: i32) { self.walks.remove(&id); } } fn main() { let mut app = WalkedApp::new(); // Create some walks let walk1_id = app.create_walk("Walk 1".to_string(), 10.0, 2.0); let walk2_id = app.create_walk("Walk 2".to_string(), 20.0, 4.0); // View all walks app.view_walks(); // Delete a walk app.delete_walk(walk1_id); // View all walks again app.view_walks(); } ``` This code defines a `Walk` struct to hold walk data, a `WalkedApp` struct to hold walk data and methods, and a `main` function to demonstrate the app's functionality. **How it works:** 1. The `Walk` struct holds the walk's ID, name, distance, and duration. 2. The `WalkedApp` struct holds a `HashMap` of walks, where each walk is identified by its ID. 3. The `create_walk` method creates a new walk and adds it to the app. 4. The `view_walks` method prints out all walks in the app. 5. The `delete_walk` method removes a walk from the app by its ID. **Example use cases:** * Create a new walk: `app.create_walk("Walk 1".to_string(), 10.0, 2.0)` * View all walks: `app.view_walks()` * Delete a walk: `app.delete_walk(walk1_id)` Note that this is a simple implementation and you may want to add more features, such as user authentication, data validation, and error handling.

Want to kickstart your project?Use the new AI Studio to create your code