I have troubles to make a good API for my small lib. For instance, I provide following function:
pub fn random_choice<'a, T>(samples: &'a Vec<T>, weights: &Vec<f64>, n: usize) -> Vec<&'a T>
But there is also an in place variant which has one argument less. Because rust doesn’t support function overloading, I had to give it a new name:
pub fn random_choice_in_place<T: Copy>(samples: &mut Vec<T>, weights: &Vec<f64>)
But T may be boxed, so I cant copy it. So I have to design a new API:
pub fn random_choice_in_place<T>(samples: &mut Vec<Box<T>>, weights: &Vec<f64>)
The problem is, I can’t overload a function in rust. Even a trait doesn’t seem to work, because there is a difference between
Vec<T>. So do I have to invent a new function name like this?
fn random_choice_in_place_boxed<T>(samples: &mut Vec<Box<T>>, weights: &Vec<f64>)
I could implement this easily in Java or C++, but I have my problems with rust. Any ideas, how I could define just one function name with different parameters?
My code is here, but the boxed variant is missing so far.