use std::collections::HashMap; use std::time::{SystemTime, Duration}; fn main() { let mut sessions: HashMap<&str, SystemTime> = HashMap::new(); sessions.insert("session1", SystemTime::now()); sessions.insert("session2", SystemTime::now() - Duration::from_secs(3600)); sessions.insert("session3", SystemTime::now() - Duration::from_secs(7200)); // Retain only sessions that are less than an hour old let one_hour = Duration::from_secs(3600); sessions.retain(|_id, &mut timestamp| { timestamp.elapsed().unwrap_or_default() < one_hour }); println!("{:?}", sessions); // Output: Only sessions less than an hour old }
Write, Run & Share Rust code online using OneCompiler's Rust online compiler for free. It's one of the robust, feature-rich online compilers for Rust language. Getting started with the OneCompiler's Rust editor is easy and fast. The editor shows sample boilerplate code when you choose language as Rust and start coding.