本文概述
if let语法用于组合if和let, 而if和let则处理与模式之一匹配的值, 而忽略其余代码。 ” match”运算符和” if let”表达式的工作方式相似。
匹配运算符的示例
fn main()
{
let a = Some(5);
match a {
Some(5) => println!("five"), _ => (), }}
输出
five
在上面的示例中, 当值等于Some(5)时, match运算符执行代码。执行第一个变量后, ” _ =>()”表达式满足match表达式。如果我们使用if let而不是match, 那么它将减少代码的长度。
if let的示例
fn main()
{
let a=Some(3);
if let Some(3)=a{
println!("three");
}
}
输出
three
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_47293.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57