The main goal is to access an owned box that is inside a tuple, e.g. the string from the (String, i32)
in the code below.
My first intention was to use a let
binding to borrow from the owned box. Borrowing works for the non-tuple case (1), but not when a tuple is involved (2).
Is my intention wrong, and if so, is there another idiomatic way to access the string?
Example code:
fn main() {
// 1. Normal borrowing
let s: String = "blub".to_string();
let sr: &str = &s; // this works
// 2. Borrowing from a tuple
let st = ("blub".to_string(), 1);
let (st_r, i): (&str, i32) = st; // error: mismatched types:
println!( "{} {} {} {}", s, sr, st_r, i);
}
The compiler error is:
error: mismatched types:
expected `(&str, i32)`,
found `(collections::string::String, _)`