I used to write Julia(https://julialang.org/), it has a nice coding type called Mutiple Dispatch. For that, the a function can have different method according to the type of parameters. An example:
struct DataSave
save1::Dict{Int32, Float32}
save2::Dict{String, Float32}
end
Base.haskey(x::DataSave, i::Int32) = haskey(x.save1, i) // dispatch method haskey on (DataSave, Inti32)
Base.haskey(x::DataSave, i::String) = haskey(x.save2, i) // dispatch method haskey on (DataSave, String)
data_save = DataSave(Dict(1 => 1f32), Dict("a" => 1f32));
haskey(data_save, Int32(1))
true
haskey(data_save, Int32(2))
false
haskey(data_save, "a")
true
I tried to realize it in Rust, I get to this:
struct DataSave {
save1: HashMap<i32, f32>,
save2: HashMap<String, f32>,
}
trait HasKey<T> {
fn has_key(&self, y: &DataSave) -> bool;
}
impl HasKey<i32> for i32 {
fn has_key(&self, y: &DataSave) -> bool {
y.save1.contains_key(self)
}
}
impl HasKey<String> for String {
fn has_key(&self, y: &DataSave) -> bool {
y.save2.contains_key(self)
}
}
impl DataSave {
fn contains_key<T: HasKey<T>>(&self, x: T) -> bool {
x.has_key(self)
}
}
let mut save1 = HashMap::new();
save1.insert(1i32, 2f32);
let mut save2 = HashMap::new();
save2.insert(String::from("a"), 2f32);
let data_save = DataSave { save1, save2 };
data_save.contains_key(1i32)
true
data_save.contains_key(2i32)
false
data_save.contains_key(String::from("a"))
true
But as can be seen, the code in Rust seems much verbose than that in Julia. So I want to ask is there a more brifer way or a official way I can do that in Rust?