3 ms·
The difference is in C++ rvalues coerce to const&, while in Rust they do not. E.x. this works int square(const int & num) { return num * num;
by mastax 10y ago
The difference is in C++ rvalues coerce to const&, while in Rust they do not.
E.x. this works
int square(const int & num) {
return num * num;
}
int main() {
square(5);
}
this does not
fn takes_ref(a: &i32) -> i32 {
*a
}
fn main() {
takes_ref(64);
}
There are still cases where you won't be able to tell if you're using a ref or not, though:
let x = returns_a_ref();
takes_ref(x);