Hello
use std::collections::{HashMap};
pub trait ContextUnit {}
#[derive(Debug)]
pub struct SimpleContextUnit {}
#[derive(Debug)]
pub struct ComplexContextUnit {}
impl ContextUnit for SimpleContextUnit {}
impl ContextUnit for ComplexContextUnit {}
#[derive(Debug)]
pub struct ContextHolder<T: ContextUnit> {
units: HashMap<String, T>
}
fn main() {
let mut units = HashMap::new();
units.insert("simple".to_owned(), SimpleContextUnit {});
// units.insert("complex".to_owned(), ComplexContextUnit {});
let immut_units = units;
let holder = ContextHolder {
units: immut_units
};
println!("{:?}", holder);
}
How can I reach polymorphism In this example?
If I uncomment the following line:
units.insert("complex".to_owned(), ComplexContextUnit {});
The program will fail.
error[E0308]: mismatched types
--> src/main.rs:49:40
|
49 | units.insert("complex".to_owned(), ComplexContextUnit {});
| ^^^^^^^^^^^^^^^^^^^^^ expected struct `SimpleContextUnit`, found struct `ComplexContextUnit`
|
= note: expected type `SimpleContextUnit`
= note: found type `ComplexContextUnit`
In OOP language I can have base class and pass it's type to map as value.
How can I do it in rust?
P.S the goal is to have a map units: HashMap<String, T>
where T: ContextUnit
And pass as a map value any struct that implements ContextUnit
Thanks