Untitled
unknown
rust
2 years ago
1.5 kB
8
Indexable
use std::collections::HashMap;
struct MultimediaType {
name: &'static str,
allowed_information: Vec<String>,
}
struct Multimedia {
name: &'static str,
multimedia_type: MultimediaType,
information: HashMap<String, String>,
}
impl Multimedia {
fn new(
name: &'static str,
multimedia_type: MultimediaType,
information: HashMap<String, String>,
) -> Multimedia {
for (k, _) in information.iter() {
if !multimedia_type.allowed_information.contains(k) {
panic!("{} is not allowed for {}", k, multimedia_type.name);
}
}
Multimedia {
name,
multimedia_type,
information,
}
}
fn print(&self) {
println!("Multimedia: {}", self.name);
println!("Type: {}", self.multimedia_type.name);
for (k, v) in self.information.iter() {
println!("{}: {}", k, v);
}
}
}
fn main() {
let type_videogame = MultimediaType {
name: "Videogame",
allowed_information: vec!["platform".to_string(), "genre".to_string()],
};
let mut information = HashMap::new();
information.insert("platform".to_string(), "Nintendo Switch".to_string());
information.insert("genre".to_string(), "Action".to_string());
let videogame = Multimedia::new(
"The Legend of Zelda: Breath of the Wild",
type_videogame,
information,
);
videogame.print();
}
Editor is loading...
Leave a Comment