Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to destructure and derefence tuple in rust

Is there a way to simplify the below code by dereferencing foo and bar on the same line as we destructure the tuple?

let a = "a";
let b = "b";
let c = (&a, &b);

// Can we dereference here?
let (foo, bar) = c;

let foo_loc = *foo;
let bar_loc = *bar;
println!("{}", foo_loc);
println!("{}", bar_loc);
like image 942
SBUK-Tech Avatar asked Oct 15 '25 10:10

SBUK-Tech


1 Answers

You can pattern match the references:

fn main() {
    let a = "a";
    let b = "b";
    let c = (&a, &b);
    let (&foo, &bar) = c;
    println!("{}", foo);
    println!("{}", bar);
}

Playground

like image 168
Netwave Avatar answered Oct 18 '25 08:10

Netwave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!