{"id":205810,"date":"2020-12-04T08:20:05","date_gmt":"2020-12-04T00:20:05","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=205810"},"modified":"2020-11-23T11:22:41","modified_gmt":"2020-11-23T03:22:41","slug":"rust-conditional-statements","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/rust-conditional-statements.html","title":{"rendered":"\u5b66\u4e60Rust \u6761\u4ef6\u8bed\u53e5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nRust \u8bed\u8a00\u662f\u4e00\u79cd\u9ad8\u6548\u3001\u53ef\u9760\u7684\u901a\u7528\u9ad8\u7ea7\u8bed\u8a00\u3002\u5176\u9ad8\u6548\u4e0d\u4ec5\u9650\u4e8e\u5f00\u53d1\u6548\u7387\uff0c\u5b83\u7684\u6267\u884c\u6548\u7387\u4e5f\u662f\u4ee4\u4eba\u79f0\u8d5e\u7684\uff0c\u662f\u4e00\u79cd\u5c11\u6709\u7684\u517c\u987e\u5f00\u53d1\u6548\u7387\u548c\u6267\u884c\u6548\u7387\u7684\u8bed\u8a00\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u5728 Rust \u8bed\u8a00\u4e2d\u7684\u6761\u4ef6\u8bed\u53e5\u4f7f\u8fd9\u79cd\u683c\u5f0f\u7684\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\nfn main() {\r\n    let number = 3;\r\n    if number < 5 {\r\n        println!(\"\u6761\u4ef6\u4e3a true\");\r\n    } else {\r\n        println!(\"\u6761\u4ef6\u4e3a false\");\r\n    }\r\n}<\/pre>\n

\u5728\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\u6709\u6761\u4ef6 if \u8bed\u53e5\uff0c\u8fd9\u4e2a\u8bed\u6cd5\u5728\u5f88\u591a\u5176\u5b83\u8bed\u8a00\u4e2d\u5f88\u5e38\u89c1\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u533a\u522b\uff1a\u9996\u5148\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f number < 5 \u4e0d\u9700\u8981\u7528\u5c0f\u62ec\u53f7\u5305\u62ec\uff08\u6ce8\u610f\uff0c\u4e0d\u9700\u8981\u4e0d\u662f\u4e0d\u5141\u8bb8\uff09\uff1b\u4f46\u662f Rust \u4e2d\u7684 if \u4e0d\u5b58\u5728\u5355\u8bed\u53e5\u4e0d\u7528\u52a0 {} \u7684\u89c4\u5219\uff0c\u4e0d\u5141\u8bb8\u4f7f\u7528\u4e00\u4e2a\u8bed\u53e5\u4ee3\u66ff\u4e00\u4e2a\u5757\u3002\u5c3d\u7ba1\u5982\u6b64\uff0cRust \u8fd8\u662f\u652f\u6301\u4f20\u7edf else-if \u8bed\u6cd5\u7684\uff1a\n\n\u5b9e\u4f8b\n\n\n

\r\nfn main() {\r\n    let a = 12;\r\n    let b;\r\n    if a > 0 {\r\n        b = 1;\r\n    }  \r\n    else if a < 0 {\r\n        b = -1;\r\n    }  \r\n    else {\r\n        b = 0;\r\n    }\r\n    println!(\"b is {}\", b);\r\n}<\/pre>\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

\r\nb \u4e3a 1<\/pre>\n

Rust \u4e2d\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u5fc5\u987b\u662f bool \u7c7b\u578b\uff0c\u4f8b\u5982\u4e0b\u9762\u7684\u7a0b\u5e8f\u662f\u9519\u8bef\u7684\uff1a<\/p>\n

\r\nfn main() { \r\n    let number = 3; \r\n    if number {   \/\/ \u62a5\u9519\uff0cexpected `bool`, found integerrustc(E0308)\r\n        println!(\"Yes\");\r\n    } \r\n} <\/pre>\n

\u867d\u7136 C\/C++ \u8bed\u8a00\u4e2d\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u7528\u6574\u6570\u8868\u793a\uff0c\u975e 0 \u5373\u771f\uff0c\u4f46\u8fd9\u4e2a\u89c4\u5219\u5728\u5f88\u591a\u6ce8\u91cd\u4ee3\u7801\u5b89\u5168\u6027\u7684\u8bed\u8a00\u4e2d\u662f\u88ab\u7981\u6b62\u7684\u3002<\/p>\n

\u7ed3\u5408\u4e4b\u524d\u7ae0\u5b66\u4e60\u7684\u51fd\u6570\u4f53\u8868\u8fbe\u5f0f\u6211\u4eec\u52a0\u4ee5\u8054\u60f3\uff1a<\/p>\n

\r\nif  { block 1 } else { block 2 } <\/condition><\/pre>\n

\u8fd9\u79cd\u8bed\u6cd5\u4e2d\u7684 { block 1 } \u548c { block 2 } \u53ef\u4e0d\u53ef\u4ee5\u662f\u51fd\u6570\u4f53\u8868\u8fbe\u5f0f\u5462\uff1f<\/p>\n

\u7b54\u6848\u662f\u80af\u5b9a\u7684\uff01\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728 Rust \u4e2d\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 if-else \u7ed3\u6784\u5b9e\u73b0\u7c7b\u4f3c\u4e8e\u4e09\u5143\u6761\u4ef6\u8fd0\u7b97\u8868\u8fbe\u5f0f (A ? B : C) \u7684\u6548\u679c\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\nfn main() {\r\n    let a = 3;\r\n    let number = if a > 0 { 1 } else { -1 };\r\n    println!(\"number \u4e3a {}\", number);\r\n}<\/pre>\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

\r\nnumber \u4e3a 1<\/pre>\n

\u6ce8\u610f\uff1a\u4e24\u4e2a\u51fd\u6570\u4f53\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u5fc5\u987b\u4e00\u6837\uff01\u4e14\u5fc5\u987b\u6709\u4e00\u4e2a else \u53ca\u5176\u540e\u7684\u8868\u8fbe\u5f0f\u5757\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728 Rust \u8bed\u8a00\u4e2d\u7684\u6761\u4ef6\u8bed\u53e5\u4f7f\u8fd9\u79cd\u683c\u5f0f\u7684\uff1a \u5b9e\u4f8b fn main() { let number = 3; i […]<\/p>\n","protected":false},"author":1470,"featured_media":205801,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-205810","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/205810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=205810"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/205810\/revisions"}],"predecessor-version":[{"id":205813,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/205810\/revisions\/205813"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/205801"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=205810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=205810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=205810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}