{"id":1619,"date":"2019-11-24T18:39:09","date_gmt":"2019-11-24T10:39:09","guid":{"rendered":"http:\/\/ykyi.net\/?p=1619"},"modified":"2019-11-24T18:39:09","modified_gmt":"2019-11-24T10:39:09","slug":"rust%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84%e5%8d%95%e4%bd%8d%e7%b1%bb%e5%9e%8bunit-type%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/ykyi.net\/?p=1619","title":{"rendered":"Rust\u8bed\u8a00\u4e2d\u7684\u5355\u4f4d\u7c7b\u578bunit type\u662f\u4ec0\u4e48"},"content":{"rendered":"<p>\u6bcf\u4e2a\u5b66\u4e60Rust\u8bed\u8a00\u7684\u8bfb\u8005\u78b0\u5230\u5355\u4f4d\u7c7b\u578bunit type ()\u7684\u65f6\u5019\u90fd\u975e\u5e38\u7591\u60d1\u8fd9\u7a76\u7adf\u662f\u4e2a\u4ec0\u4e48\u9b3c\uff1f<br \/>\n\u672c\u6587\u603b\u7ed3\u4e86stackoverflow\u4e0a\u9762\u7684\u8d34\u5b50 <a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/stackoverflow.com\/questions\/24842271\/what-is-the-purpose-of-the-unit-type-in-rust\">What is the purpose of the unit type in Rust?<\/a>\u52a0\u4e0a\u6211\u81ea\u5df1\u7684\u7406\u89e3\u3002<\/p>\n<ol>\n<li>unit type\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u6709\u4e14\u4ec5\u6709\u4e00\u4e2a\u503c\uff0c\u90fd\u5199\u6210\u5c0f\u62ec\u53f7()<\/li>\n<li>\u5355\u5143\u7c7b\u578b()\u7c7b\u4f3cc\/c++\/java\u8bed\u8a00\u4e2d\u7684void\u3002\u5f53\u4e00\u4e2a\u51fd\u6570\u5e76\u4e0d\u9700\u8981\u8fd4\u56de\u503c\u7684\u65f6\u5019\uff0cc\/c++\/java\u4e2d\u51fd\u6570\u8fd4\u56devoid\uff0crust\u5219\u8fd4\u56de()\u3002\u4f46\u8bed\u6cd5\u5c42\u9762\u4e0a\uff0cvoid\u4ec5\u4ec5\u53ea\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u8be5\u7c7b\u578b\u6ca1\u6709\u4efb\u4f55\u503c;\u800c\u5355\u4f4d\u7c7b\u578b()\u65e2\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u540c\u65f6\u53c8\u662f\u8be5\u7c7b\u578b\u7684\u503c\u3002<\/li>\n<li>\u5355\u5143\u7c7b\u578b()\u4e5f\u7c7b\u4f3cc\/c++\/java\u4e2d\u7684null\uff0c\u4f46\u5374\u6709\u5f88\u5927\u4e0d\u540c\u3002 null\u662f\u4e00\u4e2a\u7279\u6b8a\u503c\uff0c\u53ef\u4ee5\u8d4b\u7ed9\u4e0d\u53ef\u7c7b\u578b\u7684\u503c\uff0c\u4f8b\u5982java\u4e2d\u7684\u5bf9\u8c61\uff0cc\u4e2d\u6307\u5411struct\u5b9e\u4f8b\u7684\u6307\u9488\uff0cc++\u4e2d\u7684\u5bf9\u8c61\u6307\u9488\u3002\u4f46\u5728rust\u4e2d\uff0c()\u4e0d\u53ef\u4ee5\u8d4b\u503c\u7ed9\u9664\u5355\u5143\u7c7b\u578b\u5916\u7684\u5176\u5b83\u7684\u7c7b\u578b\u7684\u53d8\u91cf\uff0c()\u53ea\u80fd\u8d4b\u503c\u7ed9()\u3002<\/li>\n<li>Rust\u6807\u51c6\u5e93\u4e2d\u4f7f\u7528\u5355\u5143\u7c7b\u578b()\u7684\u4e00\u4e2a\u4f8b\u5b50\u662fHashSet\u3002\u4e00\u4e2aHashSet<t>\u53ea\u4e0d\u8fc7\u662fHashMap\u7684\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u5730\u5305\u88f9\uff0c\u5199\u4f5c\uff1aHashMap&lt;T, ()>\u3002HashMap\u7684\u7b2c\u4e8c\u4e2a\u6cdb\u578b\u7c7b\u578b\u53c2\u6570\u5373\u7528\u4e86\u5355\u5143\u7c7b\u578b()<\/li>\n<li>\u53ef\u4ee5\u7528Result&lt;(), MyErrorType>\u4ee3\u66ffOption<myErrorType>\uff0c\u67d0\u4e9b\u5f00\u53d1\u8005\u8ba4\u4e3aResult&lt;(), MyErrorType>\u8bed\u4e49\u4e0a\u80fd\u66f4\u7b80\u660e\u5730\u8868\u793a\u4e00\u4e2a\u201c\u7ed3\u679c\u201d\u3002<\/li>\n<\/ol>\n<blockquote><p>\n  You could argue that Option<myError> is &#8216;better&#8217;, but I disagree; the Result&#8217;s naming conventions mean it&#8217;s explicitly clear what&#8217;s going on.\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u6bcf\u4e2a\u5b66\u4e60Rust\u8bed\u8a00\u7684\u8bfb\u8005\u78b0\u5230\u5355\u4f4d\u7c7b\u578bunit type ()\u7684\u65f6\u5019\u90fd\u975e\u5e38\u7591\u60d1\u8fd9\u7a76\u7adf\u662f\u4e2a\u4ec0\u4e48\u9b3c\uff1f \u672c\u6587\u603b\u7ed3\u4e86stackoverflow\u4e0a\u9762\u7684\u8d34\u5b50 What is the purpose of the unit type in Rust?\u52a0\u4e0a\u6211\u81ea\u5df1\u7684\u7406\u89e3\u3002 unit type\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u6709\u4e14\u4ec5\u6709\u4e00\u4e2a\u503c\uff0c\u90fd\u5199\u6210\u5c0f\u62ec\u53f7() \u5355\u5143\u7c7b\u578b()\u7c7b\u4f3cc\/c++\/java\u8bed\u8a00\u4e2d\u7684void\u3002\u5f53\u4e00\u4e2a\u51fd\u6570\u5e76\u4e0d\u9700\u8981\u8fd4\u56de\u503c\u7684\u65f6\u5019\uff0cc\/c++\/java\u4e2d\u51fd\u6570\u8fd4\u56devoid\uff0crust\u5219\u8fd4\u56de()\u3002\u4f46\u8bed\u6cd5\u5c42\u9762\u4e0a\uff0cvoid\u4ec5\u4ec5\u53ea\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u8be5\u7c7b\u578b\u6ca1\u6709\u4efb\u4f55\u503c;\u800c\u5355\u4f4d\u7c7b\u578b()\u65e2\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u540c\u65f6\u53c8\u662f\u8be5\u7c7b\u578b\u7684\u503c\u3002 \u5355\u5143\u7c7b\u578b()\u4e5f\u7c7b\u4f3cc\/c++\/java\u4e2d\u7684null\uff0c\u4f46\u5374\u6709\u5f88\u5927\u4e0d\u540c\u3002 null\u662f\u4e00\u4e2a\u7279\u6b8a\u503c\uff0c\u53ef\u4ee5\u8d4b\u7ed9\u4e0d\u53ef\u7c7b\u578b\u7684\u503c\uff0c\u4f8b\u5982java\u4e2d\u7684\u5bf9\u8c61\uff0cc\u4e2d\u6307\u5411struct\u5b9e\u4f8b\u7684\u6307\u9488\uff0cc++\u4e2d\u7684\u5bf9\u8c61\u6307\u9488\u3002\u4f46\u5728rust\u4e2d\uff0c()\u4e0d\u53ef\u4ee5\u8d4b\u503c\u7ed9\u9664\u5355\u5143\u7c7b\u578b\u5916\u7684\u5176\u5b83\u7684\u7c7b\u578b\u7684\u53d8\u91cf\uff0c()\u53ea\u80fd\u8d4b\u503c\u7ed9()\u3002 Rust\u6807\u51c6\u5e93\u4e2d\u4f7f\u7528\u5355\u5143\u7c7b\u578b()\u7684\u4e00\u4e2a\u4f8b\u5b50\u662fHashSet\u3002\u4e00\u4e2aHashSet\u53ea\u4e0d\u8fc7\u662fHashMap\u7684\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u5730\u5305\u88f9\uff0c\u5199\u4f5c\uff1aHashMap&lt;T, ()>\u3002HashMap\u7684\u7b2c\u4e8c\u4e2a\u6cdb\u578b\u7c7b\u578b\u53c2\u6570\u5373\u7528\u4e86\u5355\u5143\u7c7b\u578b() \u53ef\u4ee5\u7528Result&lt;(), MyErrorType>\u4ee3\u66ffOption\uff0c\u67d0\u4e9b\u5f00\u53d1\u8005\u8ba4\u4e3aResult&lt;(), MyErrorType>\u8bed\u4e49\u4e0a\u80fd\u66f4\u7b80\u660e\u5730\u8868\u793a\u4e00\u4e2a\u201c\u7ed3\u679c\u201d\u3002 You could argue that Option is &#8216;better&#8217;, but I disagree; the Result&#8217;s naming conventions mean it&#8217;s explicitly clear what&#8217;s going on.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[72],"class_list":["post-1619","post","type-post","status-publish","format-standard","hentry","category-tech_articles","tag-rust"],"_links":{"self":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/1619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1619"}],"version-history":[{"count":0,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/1619\/revisions"}],"wp:attachment":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}