简体   繁体   中英

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?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM