簡體   English   中英

是否可以在 function 聲明中解構具有無可辯駁的模式的元組?

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

在 rust 我目前可以做,

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

但我無法解構簽名中的 arguments,

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

是否可以用無可辯駁的模式來解構一個元組?

你需要做的是輸入整個元組而不是其中的組件,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM