Search code examples
rusttuplespattern-matchingdestructuring

Is it possible to destructure a tuple with an irrefutable pattern in a function declaration?


In rust I can currently do,

// this function accepts k,v
fn foo(
    k: &str, v: u8
) -> bool {
    true
}

But I can not destructure the arguments in the signature,

// this function accepts (k,v) tuple
fn bar(
    (k: &str, v: u8) // notice the parens
) -> bool {
    true
}

Is it possible to destructure a tuple with an irrefutable pattern?


Solution

  • What you have to do is type the whole tuple not the components inside it,

    // this function accepts (k,v) tuple
    fn baz(
        (k, v): (&str, u8) // notice the parens
    ) -> bool {
        true
    }