{"id":492,"date":"2021-09-29T15:44:20","date_gmt":"2021-09-29T07:44:20","guid":{"rendered":"https:\/\/www.cclo.idv.tw\/blogs\/closer\/?p=492"},"modified":"2021-11-16T00:07:50","modified_gmt":"2021-11-15T16:07:50","slug":"rustlings-errors6-rs","status":"publish","type":"post","link":"https:\/\/blog.cclo.idv.tw\/?p=492","title":{"rendered":"Rustlings \u7fd2\u984c\u63a2\u8a0e\uff1aerrors6.rs"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><p>\u4e3b\u8981\u662f\u8981\u7df4\u7fd2\u5982\u4f55\u64b0\u5beb\u53ca\u61c9\u7528 error conversion\u3002<\/p>\n<p>\u6574\u500b\u7a0b\u5f0f\u662f\u8981\u5be6\u4f5c\u4e00\u500b\u65b0\u7684\u578b\u5225 <code>PositiveNonzeroInteger<\/code>\uff0c\u610f\u5373\u300c\u6b63\u6574\u6578\u300d\uff1b0 \u6216\u8ca0\u6574\u6578\u90fd\u4e0d\u662f\u5408\u6cd5\u7684\u3002\u9019\u500b\u6b63\u6574\u6578\u8981\u5f9e\u5b57\u4e32 parsing \u800c\u4f86\u3002\u5982\u679c parsing \u6642\u5c31\u51fa\u554f\u984c\uff0c\u8981\u628a <code>parse()<\/code> \u7684 error \u8f49\u63db\u6210 <code>ParsePosNonezeroError::ParseInt()<\/code>\uff1b\u800c\u5982\u679c parsing \u6c92\u554f\u984c\uff0c\u4f46\u6578\u503c\u672c\u8eab\u662f 0 \u6216\u8ca0\u6574\u6578\u7684\u8a71\uff0c\u5c31\u8981\u56de\u50b3 <code>ParsePosNonezeroError::Creation()<\/code>\uff08\u4f9d\u64da\u6578\u503c\u662f 0 \u6216\u8ca0\u6574\u6578\uff0c\u6703\u4ee3\u4e0d\u540c\u7684 error code\uff09\u3002<\/p>\n<p>Conversion \u7684\u90e8\u4efd\u5f88\u55ae\u7d14\uff1a<\/p>\n<pre><code class=\"language-rust\">impl ParsePosNonzeroError {\n    fn from_creation(err: CreationError) -&gt; ParsePosNonzeroError {\n        ParsePosNonzeroError::Creation(err)\n    }\n\n    fn from_parsing(err: ParseIntError) -&gt; ParsePosNonzeroError {\n        ParsePosNonzeroError::ParseInt(err)\n    }\n}\n<\/code><\/pre>\n<p>\u6bd4\u8f03\u6709\u8da3\u7684\u662f\u61c9\u7528\u7684\u90e8\u4efd\u3002<\/p>\n<\/div>\n\n\n\n<!--more-->\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><h2>\u89e3\u6cd5\u4e00<\/h2>\n<p>\u7528 pattern match\u3002\u7121\u8da3\u4e86\u9ede\uff0c\u4f46\u81f3\u5c11\u5f88\u6e05\u695a\u3002<\/p>\n<pre><code class=\"language-rust\">\nfn parse_pos_nonzero(s: &amp;str)\n    -&gt; Result\n{\n    let x = match s.parse() {\n        Ok(i) =&gt; i,\n        Err(e) =&gt; return Err(ParsePosNonzeroError::from_parsing(e)),\n    };\n    \/\/ PositiveNonzeroInteger::new() \u56de\u50b3\u7684 error type \u662f CreationError\uff0c\n    \/\/ \u56e0\u6b64\u9700\u8981\u7528 map_err \u8f49\u63db\u6210 ParsePosNonezeroError\u3002\n    PositiveNonzeroInteger::new(x)\n        .map_err(ParsePosNonzeroError::from_creation)\n}\n<\/code><\/pre>\n<h2>\u89e3\u6cd5\u4e8c<\/h2>\n<p>\u5229\u7528 <code>?<\/code> \u7c21\u5316\u89e3\u6cd5\u4e00\u3002\u91cd\u9ede\u662f\u8981\u5148\u505a\u5b8c <code>map_err()<\/code> \u5f8c\u624d\u52a0 <code>?<\/code>\u3002<\/p>\n<pre><code class=\"language-rust\">\nfn parse_pos_nonzero(s: &amp;str)\n    -&gt; Result\n{\n    let x = s.parse().map_err(ParsePosNonzeroError::from_parsing)?;\n    PositiveNonzeroInteger::new(x)\n        .map_err(ParsePosNonzeroError::from_creation)\n}\n<\/code><\/pre>\n<h2>\u89e3\u6cd5\u4e09<\/h2>\n<p>\u60f3\u8981\u7528\u4e00\u500b expression \u89e3\u6c7a\uff1f\u9019\u4e0d\u5c31\u4f86\u4e86\u55ce\uff1f\u7528 <code>and_then()<\/code> \u4f86\u4e32\u63a5\u3002<\/p>\n<pre><code class=\"language-rust\">\nfn parse_pos_nonzero(s: &amp;str)\n    -&gt; Result\n{\n    s.parse()\n        .map_err(ParsePosNonzeroError::from_parsing)\n        .and_then(|x| {\n            PositiveNonzeroInteger::new(x)\n                .map_err(ParsePosNonzeroError::from_creation)\n        })\n}\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[41,42],"table_tags":[],"class_list":["post-492","post","type-post","status-publish","format-standard","hentry","category--computer-science","tag-rust","tag-rustlings"],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=492"}],"version-history":[{"count":5,"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/492\/revisions"}],"predecessor-version":[{"id":618,"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/492\/revisions\/618"}],"wp:attachment":[{"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=492"},{"taxonomy":"table_tags","embeddable":true,"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftable_tags&post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}