{"id":8662,"date":"2022-07-04T09:38:00","date_gmt":"2022-07-04T00:38:00","guid":{"rendered":"https:\/\/taki-lab.site\/bocci\/?p=8662"},"modified":"2022-07-03T09:40:55","modified_gmt":"2022-07-03T00:40:55","slug":"rust%e5%8b%89%e5%bc%b7%e4%b8%ad%e3%80%817-3%e3%81%ae%e7%a9%8d%e3%81%bf%e4%b8%8a%e3%81%92","status":"publish","type":"post","link":"https:\/\/taki-lab.site\/bocci\/?p=8662","title":{"rendered":"RUST\u52c9\u5f37\u4e2d\u30017\/3\u306e\u7a4d\u307f\u4e0a\u3052"},"content":{"rendered":"\n<p>\u9006\u30dd\u30fc\u30e9\u30f3\u30c9\u8a18\u6cd5\u8a08\u7b97\u6a5f\u306banyhow\u30af\u30ec\u30fc\u30c8\u3092\u5b9f\u88c5\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-\u81ea\u5206\u3001\u307c\u3063\u3061\u3067\u3059\u304c\u4f55\u304b\uff1f wp-block-embed-\u81ea\u5206\u3001\u307c\u3063\u3061\u3067\u3059\u304c\u4f55\u304b\uff1f\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"3b39BsQdeZ\"><a href=\"https:\/\/taki-lab.site\/bocci\/?p=8208\">RUST\u52c9\u5f37\u4e2d\u30014\/23\u306e\u7a4d\u307f\u4e0a\u3052<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;RUST\u52c9\u5f37\u4e2d\u30014\/23\u306e\u7a4d\u307f\u4e0a\u3052&#8221; &#8212; \u81ea\u5206\u3001\u307c\u3063\u3061\u3067\u3059\u304c\u4f55\u304b\uff1f\" src=\"https:\/\/taki-lab.site\/bocci\/?p=8208&#038;embed=true#?secret=PrNkUBfXrT#?secret=3b39BsQdeZ\" data-secret=\"3b39BsQdeZ\" width=\"474\" height=\"267\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-\u81ea\u5206\u3001\u307c\u3063\u3061\u3067\u3059\u304c\u4f55\u304b\uff1f wp-block-embed-\u81ea\u5206\u3001\u307c\u3063\u3061\u3067\u3059\u304c\u4f55\u304b\uff1f\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"dmBBBvmcG9\"><a href=\"https:\/\/taki-lab.site\/bocci\/?p=8476\">RUST\u52c9\u5f37\u4e2d\u30016\/4\u306e\u7a4d\u307f\u4e0a\u3052<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;RUST\u52c9\u5f37\u4e2d\u30016\/4\u306e\u7a4d\u307f\u4e0a\u3052&#8221; &#8212; \u81ea\u5206\u3001\u307c\u3063\u3061\u3067\u3059\u304c\u4f55\u304b\uff1f\" src=\"https:\/\/taki-lab.site\/bocci\/?p=8476&#038;embed=true#?secret=BosT0MCQtT#?secret=dmBBBvmcG9\" data-secret=\"dmBBBvmcG9\" width=\"474\" height=\"267\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>use anyhow::{bail, ensure, Context, Result};\r\n\r\nuse clap::Parser;\r\nuse std::fs::File;\r\nuse std::io::{stdin, BufRead, BufReader};\r\n\r\nstruct RpnCalculator(bool);\r\n\r\nimpl RpnCalculator {\r\n    pub fn new(verbose: bool) -> Self {\r\n        Self(verbose)\r\n    }\r\n\r\n    pub fn eval(&amp;self, formula: &amp;str) -> Result&lt;i32> {\r\n        let mut tokens = formula.split_whitespace().rev().collect::&lt;Vec&lt;_>>();\r\n        self.eval_inner(&amp;mut tokens)\r\n    }\r\n\r\n    fn eval_inner(&amp;self, tokens: &amp;mut Vec&lt;&amp;str>) -> Result&lt;i32> {\r\n        let mut stack = Vec::new();\r\n        let mut pos = 0;\r\n\r\n        while let Some(token) = tokens.pop() {\r\n            pos += 1;\r\n\r\n            if let Ok(x) = token.parse::&lt;i32>() {\r\n                stack.push(x);\r\n            } else {\r\n                let y = stack.pop().context(format!(\"invalid syntax at {}\", pos))?;\r\n                let x = stack.pop().context(format!(\"invalid syntax at {}\", pos))?;\r\n                let res = match token {\r\n                    \"+\" => x + y,\r\n                    \"-\" => x - y,\r\n                    \"*\" => x * y,\r\n                    \"\/\" => x \/ y,\r\n                    \"%\" => x % y,\r\n                    _ => bail!(\"invalid token at {}\", pos),\r\n                };\r\n                stack.push(res);\r\n            }\r\n\r\n            if self.0 {\r\n                println!(\"{:?} {:?}\", tokens, stack);\r\n            }\r\n        }\r\n\r\n        ensure!(stack.len() == 1, \"invalid synyax\");\r\n\r\n        Ok(stack&#91;0])\r\n    }\r\n}\r\n\r\n#&#91;derive(Parser, Debug)]\r\n#&#91;clap(\r\n    name = \"My RPM program\",\r\n    version = \"1.0.0\",\r\n    author = \"Your name\",\r\n    about = \"Super awesome sample RPM calculator\"\r\n)]\r\nstruct Opts {\r\n    #&#91;clap(short, long)]\r\n    verbose: bool,\r\n\r\n    #&#91;clap(name = \"FILE\")]\r\n    formula_file: Option&lt;String>,\r\n}\r\n\r\n\r\nfn main() -> Result&lt;()> {\r\n    let opts = Opts::parse();\r\n\r\n    if let Some(path) = opts.formula_file {\r\n        \/\/ \u30d5\u30a1\u30a4\u30eb\u304b\u3089\u8aad\u307f\u51fa\u3059\r\n        let f = File::open(path)?;\r\n        let reader = BufReader::new(f);\r\n        run(reader, opts.verbose)\r\n    } else {\r\n        \/\/ \u6a19\u6e96\u5165\u529b\u304b\u3089\u8aad\u307f\u51fa\u3059\r\n        let stdin = stdin();\r\n        let reader = stdin.lock();\r\n        run(reader, opts.verbose)\r\n    }\r\n}\r\n\r\nfn run&lt;R: BufRead>(reader: R, verbose: bool) -> Result&lt;()> {\r\n    let calc = RpnCalculator::new(verbose);\r\n\r\n    for line in reader.lines() {\r\n        let line = line?;\r\n        match calc.eval(&amp;line) {\r\n            Ok(answer) => println!(\"{}\", answer),\r\n            Err(e) => eprintln!(\"{:#?}\", e),\r\n        }\r\n    }\r\n\r\n    Ok(())\r\n}\r\n\r\n#&#91;cfg(test)]\r\nmod tests {\r\n    use super::*;\r\n\r\n    #&#91;test]\r\n    fn test_ok() {\r\n        let calc = RpnCalculator::new(false);\r\n        assert_eq!(calc.eval(\"5\"), 5);\r\n        assert_eq!(calc.eval(\"50\"), 50);\r\n        assert_eq!(calc.eval(\"-50\"), -50);\r\n\r\n        assert_eq!(calc.eval(\"2 3 +\"), 5);\r\n        assert_eq!(calc.eval(\"2 3 *\"), 6);\r\n        assert_eq!(calc.eval(\"2 3 -\"), -1);\r\n        assert_eq!(calc.eval(\"2 3 \/\"), 0);\r\n        assert_eq!(calc.eval(\"2 3 %\"), 2);\r\n    }\r\n\r\n    #&#91;test]\r\n    #&#91;should_panic]\r\n    fn test_ng() {\r\n        let calc = RpnCalculator::new(false);\r\n        assert!(calc.eval(\"\").is_err());\r\n        assert!(calc.eval(\"1 1 1 +\").is_err());\r\n        assert!(calc.eval(\"+ 1 1\").is_err());\r\n    }\r\n}\r\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9006\u30dd\u30fc\u30e9\u30f3\u30c9\u8a18\u6cd5\u8a08\u7b97\u6a5f\u306banyhow\u30af\u30ec\u30fc\u30c8\u3092\u5b9f\u88c5\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2542,3],"tags":[],"class_list":["post-8662","post","type-post","status-publish","format-standard","hentry","category-rust","category-3"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8SDbY-2fI","jetpack-related-posts":[{"id":8208,"url":"https:\/\/taki-lab.site\/bocci\/?p=8208","url_meta":{"origin":8662,"position":0},"title":"RUST\u52c9\u5f37\u4e2d\u30014\/23\u306e\u7a4d\u307f\u4e0a\u3052","author":"taki","date":"2022\u5e744\u670826\u65e5","format":false,"excerpt":"https:\/\/taki-lab.site\/bocci\/?p=8084 \u524d\u56de\u306e\u7d9a\u304d\u3002 \u9006\u30dd\u30fc\u30e9\u30f3\u30c9\u8a18\u2026","rel":"","context":"\u51fa\u6765\u4e8b","block_context":{"text":"\u51fa\u6765\u4e8b","link":"https:\/\/taki-lab.site\/bocci\/?cat=9"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":8084,"url":"https:\/\/taki-lab.site\/bocci\/?p=8084","url_meta":{"origin":8662,"position":1},"title":"RUST\u52c9\u5f37\u4e2d\u30014\/3\u306e\u7a4d\u307f\u4e0a\u3052","author":"taki","date":"2022\u5e744\u67083\u65e5","format":false,"excerpt":"https:\/\/taki-lab.site\/bocci\/?p=8047 \u524d\u56de\u306e\u7d9a\u304d\u3002 \u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u2026","rel":"","context":"Rust","block_context":{"text":"Rust","link":"https:\/\/taki-lab.site\/bocci\/?cat=2542"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":8476,"url":"https:\/\/taki-lab.site\/bocci\/?p=8476","url_meta":{"origin":8662,"position":2},"title":"RUST\u52c9\u5f37\u4e2d\u30016\/4\u306e\u7a4d\u307f\u4e0a\u3052","author":"taki","date":"2022\u5e746\u67085\u65e5","format":false,"excerpt":"https:\/\/taki-lab.site\/bocci\/?p=8454 \u524d\u56de\u306e\u7d9a\u304d\u3002 anyhow\u30af\u2026","rel":"","context":"Rust","block_context":{"text":"Rust","link":"https:\/\/taki-lab.site\/bocci\/?cat=2542"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":8280,"url":"https:\/\/taki-lab.site\/bocci\/?p=8280","url_meta":{"origin":8662,"position":3},"title":"RUST\u52c9\u5f37\u4e2d\u30015\/4\u306e\u7a4d\u307f\u4e0a\u3052","author":"taki","date":"2022\u5e745\u67085\u65e5","format":false,"excerpt":"https:\/\/taki-lab.site\/bocci\/?p=8208 \u524d\u56de\u306e\u7d9a\u304d\u3002 \u524d\u56de\u4f5c\u6210\u3057\u305f\u30d7\u2026","rel":"","context":"Rust","block_context":{"text":"Rust","link":"https:\/\/taki-lab.site\/bocci\/?cat=2542"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":8638,"url":"https:\/\/taki-lab.site\/bocci\/?p=8638","url_meta":{"origin":8662,"position":4},"title":"RUST\u52c9\u5f37\u4e2d\u30016\/26\u306e\u7a4d\u307f\u4e0a\u3052","author":"taki","date":"2022\u5e746\u670828\u65e5","format":false,"excerpt":"\u524d\u56de\u306e\u7d9a\u304d https:\/\/taki-lab.site\/bocci\/?p=8476 \u4eca\u56de\u306fthise\u2026","rel":"","context":"Rust","block_context":{"text":"Rust","link":"https:\/\/taki-lab.site\/bocci\/?cat=2542"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9984,"url":"https:\/\/taki-lab.site\/bocci\/?p=9984","url_meta":{"origin":8662,"position":5},"title":"RUST\u52c9\u5f37\u4e2d\u30011\/17\u306e\u7a4d\u307f\u4e0a\u3052","author":"taki","date":"2023\u5e741\u670817\u65e5","format":false,"excerpt":"https:\/\/taki-lab.site\/bocci\/?p=9573 \u30b5\u30d6\u30b9\u30af\u30ea\u30d7\u30b7\u30e7\u30f3\u51e6\u7406\u3092\u5b9f\u88c5\u2026","rel":"","context":"Rust","block_context":{"text":"Rust","link":"https:\/\/taki-lab.site\/bocci\/?cat=2542"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/taki-lab.site\/bocci\/wp-content\/uploads\/2023\/01\/ice_video_20230117-110203.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/taki-lab.site\/bocci\/wp-content\/uploads\/2023\/01\/ice_video_20230117-110203.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/taki-lab.site\/bocci\/wp-content\/uploads\/2023\/01\/ice_video_20230117-110203.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"jetpack_likes_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=\/wp\/v2\/posts\/8662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8662"}],"version-history":[{"count":1,"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=\/wp\/v2\/posts\/8662\/revisions"}],"predecessor-version":[{"id":8664,"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=\/wp\/v2\/posts\/8662\/revisions\/8664"}],"wp:attachment":[{"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}