This something that bothered me more than once. Consider this code:
struct MyStruct{};
fn do_something(data: &[MyStruct]) {
// something useful here
}
fn main() {
let objects = vec![MyStruct{}, MyStruct{}];
do_something(&objects); // <-- THIS IS OK;
// but what happens if I have a vec of refs? Couldn't it work the same?
let refs = vec![&MyStruct{}, &MyStruct{}];
do_something(&refs); // <-- THIS DOES NOT COMPILE;
}
If I change the signature of do_something
to do_something(data: &[&MyStruct])
, then it compiles with &refs
but not with &objects
.
Anyway, from the point of view of the do_something
function, it seems to me that there is no difference between &[MyStruct]
and &[&MyStruct]
, so, couldn't it just accept both?
What is the simplest signature for a function that would accept both the slices transparently?