My (limited) understanding is that format!("{}", v)
and format!("{}", &v)
are essentially identical, and should generate identical assembly code and identical performance results. This is based on reading Does println! borrow or own the variable?). Yet, when comparing these two functions, the assembly is somewhat different. Is this expected, or is this a missing Rust compiler optimization? Or is there some edge case in which it is important to generate different code?
pub fn bar(j: i32) -> String {
format!("bar {}", j)
}
// vs
pub fn foo(i: i32) -> String {
format!("foo {}", &i)
}
Benchmarks
Running benchmarks produces ~6% difference in performance: