Exercise 22 / 24: Feature Flag Index
// Iterators of Code 2026 — Day 22: Feature Flag Index
// Challenge: Build the lookup table without a manual insertion loop.
// Own each flag name in the result. If a name appears more than once, the
// LAST value wins. The resulting table has no defined iteration order.
use std::collections::HashMap;
fn index_flags(flags: &[(&str, bool)]) -> HashMap<String, bool> {
let mut index = HashMap::new();
for (name, enabled) in flags {
index.insert(name.to_string(), *enabled);
}
index
}
fn main() {
let flags = index_flags(&[
("dark_mode", true),
("beta_search", false),
("dark_mode", false),
("beta_search", true),
]);
assert_eq!(flags.len(), 2);
assert_eq!(flags.get("dark_mode"), Some(&false));
assert_eq!(flags.get("beta_search"), Some(&true));
assert_eq!(flags.get("unknown"), None);
assert!(index_flags(&[]).is_empty());
assert_eq!(
index_flags(&[("", false), ("", true)]),
HashMap::from([(String::new(), true)])
);
}
Your solution
Standalone .rs / std only. Stored, not executed.
Editing access uses this browser's cookie. Clearing cookies loses access.