2014-10-19 57 views
0

如何獲得match語句中通配符臂的值?獲取通配符臂的值

例如:

let a = 1i; 
let b = 2i; 
match a.cmp(&b) { 
    Greater => println!("is greater"), 
    _ => println!("is {}", _) // error: unexpected token: `_` 
} 

我希望的東西不是存儲enum在一個變量被匹配清潔:

let a = 1i; 
let b = 2i; 
let ord = a.cmp(&b); 
match ord { 
    Greater => println!("is greater"), 
    _ => println!("is {}", ord) 
} 

回答

1

這是你問什麼?

let a = 1i; 
let b = 2i; 
match a.cmp(&b) { 
    Greater => println!("is greater"), 
    e => println!("is {}", e) 
} 
+0

謝謝,不知道任何標識符可以用作通配符。 – August 2014-10-19 16:15:58

+0

是的,但有一個問題可以解決:如果您嘗試在不使用'use'導入的'enum'變體上匹配,您可能會得到[關於無法訪問的模式的錯誤](http://is.gd/ Dep83r)。在這裏,'Greater'是自動導入的,因爲它是前奏的一部分。 – 2014-10-19 17:48:56