I've been using destructuring to create references into nested data, just for practice. I created a method that uses destructuring to break apart a borrowed tuple:
fn print_strings((x, y): &(String, String)) {
println!("x: {}, y: {}", x, y);
}
fn main() {
print_strings(&("foo".to_string(), "bar".to_string()));
}
If I add let () = x
to the first line of the print_strings()
function, the compiler tells me that x has type &String
. This led me to believe that this is an instance of deref coercion, that the compiler was translating the &(String, String)
from main()
into a (&String, &String)
to be used in print_strings()
.
But then I looked up the documentation for tuple
and didn't find an implementation of Deref
. So my question is, how does the compiler expand this code?
My hypothesis is that something like this happens:
fn print_strings(a: &(String, String)) {
let x = &(a.0);
let y = &(a.1);
println!("x: {}, y: {}", x, y);
}
EDIT:
I suppose this can't be deref coercion because the signature of the function matches the arguments being passed in by main()