Just out of curiosity, not problem I met.
As my understanding, the type signature means foo
takes a reference to A
as input. But &v
pattern undoes it, so foo
's parameter is actually passed by value.
When would this useage be necessary to use?
If you need to implement a trait that takes inputs by reference, but you want to use the inputs by value.
Note that this only works for Copy
types
trait Trait<T> {
fn by_ref(t: &T);
}
impl Trait<i32> for () {
fn by_ref(&t: &i32) {
do_work(t)
}
}
fn do_work(_: i32) {}
1 Like
So this "usage" does not take ownership?
No, but with Copy
types ownership doesn't really matter anyways. Every time they move, you still get to use the old value.
1 Like
system
Closed
August 13, 2019, 3:13am
5
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.