{"id":8208,"date":"2022-04-26T15:25:00","date_gmt":"2022-04-26T06:25:00","guid":{"rendered":"https:\/\/taki-lab.site\/bocci\/?p=8208"},"modified":"2022-04-23T15:27:33","modified_gmt":"2022-04-23T06:27:33","slug":"rust%e5%8b%89%e5%bc%b7%e4%b8%ad%e3%80%814-23%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=8208","title":{"rendered":"RUST\u52c9\u5f37\u4e2d\u30014\/23\u306e\u7a4d\u307f\u4e0a\u3052"},"content":{"rendered":"\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=\"KdNxrJJew4\"><a href=\"https:\/\/taki-lab.site\/bocci\/?p=8084\">RUST\u52c9\u5f37\u4e2d\u30014\/3\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\/3\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=8084&#038;embed=true#?secret=q9Y1fiNLNN#?secret=KdNxrJJew4\" data-secret=\"KdNxrJJew4\" width=\"474\" height=\"267\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>\u524d\u56de\u306e\u7d9a\u304d\u3002<\/p>\n\n\n\n<p>\u9006\u30dd\u30fc\u30e9\u30f3\u30c9\u8a18\u6cd5\u306e\u8a08\u7b97\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use clap::Parser;\r\nuse std::fs::File;\r\nuse std::io::{stdin, BufRead, BufReader};\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\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) -> 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>) -> i32 {\r\n        let mut stack = Vec::new();\r\n\r\n        while let Some(token) = tokens.pop() {\r\n            if let Ok(x) = token.parse::&lt;i32>() {\r\n                stack.push(x);\r\n            } else {\r\n                let y = stack.pop().expect(\"invalid syntax\");\r\n                let x = stack.pop().expect(\"invalid syntax\");\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                    _ => panic!(\"invalid token\"),\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        if stack.len() == 1 {\r\n            stack&#91;0]\r\n        } else {\r\n            panic!(\"invalid syntax\")\r\n        }\r\n    }\r\n}\r\n\r\nfn main() {\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).unwrap();\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) {\r\n    let calc = RpnCalculator::new(verbose);\r\n\r\n    for line in reader.lines() {\r\n        let line = line.unwrap();\r\n        let answer = calc.eval(&amp;line);\r\n        println!(\"{}\", answer);\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cargo run -- -v\r\n   Compiling samplecli v0.1.0 (\/home\/taki\/rust\/samplecli)\r\n    Finished dev &#91;unoptimized + debuginfo] target(s) in 0.76s\r\n     Running `target\/debug\/samplecli -v`\r\n1 1 1 + +\r\n&#91;\"+\", \"+\", \"1\", \"1\"] &#91;1]\r\n&#91;\"+\", \"+\", \"1\"] &#91;1, 1]\r\n&#91;\"+\", \"+\"] &#91;1, 1, 1]\r\n&#91;\"+\"] &#91;1, 2]\r\n&#91;] &#91;3]\r\n3<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u56de\u306e\u7d9a\u304d\u3002 \u9006\u30dd\u30fc\u30e9\u30f3\u30c9\u8a18\u6cd5\u306e\u8a08\u7b97\u51e6\u7406\u3092\u5b9f\u88c5\u3057\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":[9],"tags":[],"class_list":["post-8208","post","type-post","status-publish","format-standard","hentry","category-9"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8SDbY-28o","jetpack-related-posts":[{"id":8662,"url":"https:\/\/taki-lab.site\/bocci\/?p=8662","url_meta":{"origin":8208,"position":0},"title":"RUST\u52c9\u5f37\u4e2d\u30017\/3\u306e\u7a4d\u307f\u4e0a\u3052","author":"taki","date":"2022\u5e747\u67084\u65e5","format":false,"excerpt":"\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 https:\/\/taki-lab.\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":8084,"url":"https:\/\/taki-lab.site\/bocci\/?p=8084","url_meta":{"origin":8208,"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":8047,"url":"https:\/\/taki-lab.site\/bocci\/?p=8047","url_meta":{"origin":8208,"position":2},"title":"Rust\u52c9\u5f37\u4e2d\u30013\/27\u306e\u7a4d\u307f\u4e0a\u3052","author":"taki","date":"2022\u5e743\u670827\u65e5","format":false,"excerpt":"\u3053\u306e\u672c\u3092\u4f7f\u7528\u3057\u3066\u52c9\u5f37\u3057\u3066\u3044\u307e\u3059\u3002 \u672c\u65e5\u306f\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u304b\u3089\u5f15\u6570\u3092\u53d6\u5f97\u3059\u308b\u51e6\u7406\u3002 Clap\u3092\u4f7f\u7528\u3059\u308b\u65b9\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":8208,"position":3},"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":[]},{"id":8638,"url":"https:\/\/taki-lab.site\/bocci\/?p=8638","url_meta":{"origin":8208,"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":8476,"url":"https:\/\/taki-lab.site\/bocci\/?p=8476","url_meta":{"origin":8208,"position":5},"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":[]}],"jetpack_likes_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=\/wp\/v2\/posts\/8208","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=8208"}],"version-history":[{"count":1,"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=\/wp\/v2\/posts\/8208\/revisions"}],"predecessor-version":[{"id":8210,"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=\/wp\/v2\/posts\/8208\/revisions\/8210"}],"wp:attachment":[{"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/taki-lab.site\/bocci\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}