{"id":54,"date":"2026-04-04T22:18:01","date_gmt":"2026-04-04T14:18:01","guid":{"rendered":"https:\/\/blog.shufue.top\/?p=54"},"modified":"2026-04-04T22:18:01","modified_gmt":"2026-04-04T14:18:01","slug":"rust6%e6%9b%b4%e5%a4%9a%e9%9b%86%e5%90%88%e5%92%8c%e6%9b%b4%e5%a4%9a%e9%94%99%e8%af%af%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/blog.shufue.top\/?p=54","title":{"rendered":"[Rust]6\u66f4\u591a\u96c6\u5408\u548c\u66f4\u591a\u9519\u8bef\u5904\u7406"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u672c\u7ae0\u6897\u6982<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Other collections\u2014more complex and interesting  ones this time<\/li>\n\n\n\n<li>The question mark operator\u2014just type&nbsp;?&nbsp;to handle errors<\/li>\n\n\n\n<li>&nbsp;When panic and unwrap are good<\/li>\n<\/ul>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading is-style-default\"><a href=\"https:\/\/rustycab.github.io\/LearnRustEasy\/chapter_3\/chapter_3_15_3.html#3153-hashmap\u54c8\u5e0c\u6620\u5c04\">\u96c6\u5408<\/a><\/h2>\n\n\n\n<p>\u4f7f\u7528\u5176\u5b83\u66f4\u591a\u7684\u96c6\u5408\u7c7b\u578b\u9700\u5f15\u7528std::collections<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use std::collection<\/code><\/pre>\n\n\n\n<p>\u5177\u4f53\u4f7f\u7528\u6807\u51c6\u5e93\u90a3\u4e9b\u6a21\u5757\u53ef\u4ee5\u53c2\u8003 <a href=\"http:\/\/mng.bz\/27yd \">http:\/\/mng.bz\/27yd <\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">HashMap \u54c8\u5e0c\u8868<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pub struct HashMap&lt;K, V, S = RandomState, A: Allocator = Global&gt; { \/* private fields *\/ }<\/code><\/pre>\n\n\n\n<p>HashMap \u662f\u6563\u5217\u8868\u662f\u4e00\u79cd\u5229\u7528\u54c8\u5e0c\u51fd\u6570\u5b9e\u73b0&nbsp;<strong>key \u2192 value \u5feb\u901f\u6620\u5c04<\/strong>&nbsp;\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5e73\u5747\u67e5\u8be2\u590d\u6742\u5ea6\u4e3a&nbsp;<strong>O(1)<\/strong>\u3002<\/p>\n\n\n\n<p>HashMap\u7684\u5b58\u50a8\u662f\u65e0\u9700\u7684<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use std::collections::HashMap;\n\nstruct City {\n    name : String,\n    population : HashMap&lt;i32,i32&gt;,\n}\n\nfn main() {\n    let mut taillin = City {\n        name:   \"Tallinn\".to_string(),\n        population: HashMap::new(), \n    };\n\n    taillin.population.insert(2020, 437_619);\n    taillin.population.insert(1372, 3_250);\n    taillin.population.insert(1851, 24_000);\n\n    for (year,population) in taillin.population  {\n        println!(\"In {year},Tallinn had a population of {population},\");\n        \n    }\n}<\/code><\/pre>\n\n\n\n<p>HashMap\u7ec4\u6210\u4e3b\u8981\u662f\u7531Key\u548cValue\u7ec4\u6210\uff0c\u7531\u4e00\u4e2aKey\u5bf9\u5e94\u4e00\u4e2aValue<\/p>\n\n\n\n<p><code>.get()<\/code>\u51fd\u6570 \u7528\u4e8e\u901a\u8fc7Value\u7684\u5230Key\uff0c\u5982\u679c\u5f97\u5230\u8fd4\u56de<code>Some(value)<\/code>\u6ca1\u6709\u5f97\u5230\u5219\u8fd4\u56deNone<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u6f14\u793a\u4ee3\u7801<\/summary>\n<pre class=\"wp-block-code\"><code>use std::collections::HashMap;\nfn main() {\n    let canadian_cities = vec!&#91;\"Calgary\", \"Vancouver\", \"Gimli\"];\n    let german_cities = vec!&#91;\"Karlsruhe\", \"Bad Doberan\", \"Bielefeld\"];\n    let mut city_hashmap = HashMap::new();\n    for city in canadian_cities {\n        city_hashmap.insert(city, \"Canada\");\n    }\n    for city in german_cities {\n        city_hashmap.insert(city, \"Germany\");\n    }\n    println!(\"{:?}\", city_hashmap&#91;\"Bielefeld\"]);\n    println!(\"{:?}\", city_hashmap.get(\"Bielefeld\"));\n    println!(\"{:?}\", city_hashmap.get(\"Bielefeldd\"));\n}\n\n\/*\n\"Germany\"\nSome(\"Germany\")\nNone\n*\/<\/code><\/pre>\n<\/details>\n\n\n\n<p><code>.insert()<\/code>\u51fd\u6570 \u7528\u4e8e\u63d2\u5165\u65b0\u7684Value\u5230\u5df2\u6709\u7684Key\u4e2d\uff0c\u5982\u679c\u6210\u529f\u8986\u76d6\u540e\u4f1a\u8fd4\u56de\u88ab\u66ff\u6362\u7684\u65e7\u503c\u4ee5<code>Option()<\/code>\u5f62\u5f0f\u8fd4\u56de<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u6f14\u793a\u4ee3\u7801<\/summary>\n<pre class=\"wp-block-code\"><code>use std::collections::HashMap;\nfn main() {\n    let mut book_hashmap = HashMap::new();\n    let mut old_hashmap_values = Vec::new();\n    let hashmap_entries = &#91;\n        (1, \"L'Allemagne Moderne\"),\n        (1, \"Le Petit Prince\"),\n        (1, \"\uc100\ub3c4\uc6b0 \uc624\ube0c \uc720\uc5b4 \uc2a4\ub9c8\uc77c\"),\n        (1, \"Eye of the World\"),\n    ];\n\n    for (key, value) in hashmap_entries {\n        if let Some(old_value) = book_hashmap.insert(key, value) {\n        println!(\"Overwriting {old_value} with {value}!\");\n        old_hashmap_values.push(old_value);\n        }\n    }\n    println!(\"All old values:{old_hashmap_values:?}\");\n}\n\/*\nOverwriting L'Allemagne Moderne with Le Petit Prince!\nOverwriting Le Petit Prince with \uc100\ub3c4\uc6b0 \uc624\ube0c \uc720\uc5b4 \uc2a4\ub9c8\uc77c!\nOverwriting \uc100\ub3c4\uc6b0 \uc624\ube0c \uc720\uc5b4 \uc2a4\ub9c8\uc77c with Eye of the World!\nAll old values: &#91;\"L'Allemagne Moderne\", \"Le Petit Prince\", \"\uc100\ub3c4\uc6b0 \uc624\ube0c \uc720\uc5b4 \uc2a4\n\ub9c8\uc77c\"]\n*\/<\/code><\/pre>\n<\/details>\n\n\n\n<p><code>.entry()<\/code>\u51fd\u6570  <a href=\"https:\/\/mng.bz\/1JXV\">https:\/\/mng.bz\/1JXV<\/a>  \u7528\u6765\u5728\u4e00\u6b21\u67e5\u627e\u4e2d\u540c\u65f6\u5904\u7406\u201c\u5b58\u5728 \/ \u4e0d\u5b58\u5728\u201d\u4e24\u79cd\u60c5\u51b5\uff0c\u907f\u514d\u91cd\u590d\u67e5\u627e\uff0c\u63d0\u9ad8\u6027\u80fd\u5e76\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Entry&lt;K, V&gt; {\n    Occupied(OccupiedEntry&lt;K, V&gt;),\n    Vacant(VacantEntry&lt;K, V&gt;),\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.or_insert()<\/code> \u786e\u4fdd\u6761\u76ee\u4e2d\u5b58\u5728\u503c\uff0c\u5982\u679c\u4e3a\u7a7a\u5219\u63d2\u5165\u9ed8\u8ba4\u503c\uff0c\u5e76\u8fd4\u56de\u6761\u76ee\u4e2d\u503c\u7684\u53ef\u53d8\u5f15\u7528\u3002<\/li>\n\n\n\n<li><code>.or_insert_with()<\/code> \u786e\u4fdd\u6761\u76ee\u4e2d\u5b58\u5728\u503c\uff0c\u5982\u679c\u4e3a\u7a7a\u5219\u63d2\u5165\u9ed8\u8ba4\u51fd\u6570\u7684\u7ed3\u679c\uff0c\u5e76\u8fd4\u56de\u6761\u76ee\u4e2d\u503c\u7684\u53ef\u53d8\u5f15\u7528\u3002<\/li>\n\n\n\n<li><code>.or_insert_with_key()<\/code> \u786e\u4fdd\u6761\u76ee\u4e2d\u5b58\u5728\u503c\uff0c\u5982\u679c\u6761\u76ee\u4e3a\u7a7a\uff0c\u5219\u63d2\u5165\u9ed8\u8ba4\u51fd\u6570\u7684\u7ed3\u679c\u3002\u6b64\u65b9\u6cd5\u5141\u8bb8\u901a\u8fc7\u5411\u9ed8\u8ba4\u51fd\u6570\u63d0\u4f9b\u5728&nbsp;<code>.entry(key)<\/code>&nbsp;\u65b9\u6cd5\u8c03\u7528\u671f\u95f4\u79fb\u52a8\u7684\u952e\u7684\u5f15\u7528\uff0c\u6765\u751f\u6210\u952e\u6d3e\u751f\u7684\u503c\u8fdb\u884c\u63d2\u5165\u3002<\/li>\n<\/ul>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u6f14\u793a\u4ee3\u78011<\/summary>\n<pre class=\"wp-block-code\"><code>use std::collections::HashMap;\nfn main() {\n    let book_collection = vec!&#91;\n        \"L'Allemagne Moderne\",\n        \"Le Petit Prince\",\n        \"Eye of the World\",\n        \"Eye of the World\",\n    ];\n    let mut book_hashmap = HashMap::new();\n\n    for book in book_collection {\n        let return_value = book_hashmap.entry(book).or_insert(0);\n        *return_value += 1;\n    }   \n\n    for (book, number) in book_hashmap {\n        println!(\"{book}, {number}\");\n    }\n}<\/code><\/pre>\n<\/details>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u6f14\u793a\u4ee3\u78012<\/summary>\n<pre class=\"wp-block-code\"><code>use std::collections::HashMap;\nfn main() {\n    let data = vec!&#91;\n        (\"male\", 9),\n        (\"female\", 5),\n        (\"male\", 0),\n        (\"female\", 6),\n        (\"female\", 5),\n        (\"male\", 10),\n    ];\n    let mut survey_hash = HashMap::new();\n    for item in data {\n        survey_hash.entry(item.0).or_insert(Vec::new()).push(item.1);\n    }\n    for (male_or_female, numbers) in survey_hash {\n        println!(\"{male_or_female}: {numbers:?}\");\n    }\n}<\/code><\/pre>\n<\/details>\n\n\n\n<h4 class=\"wp-block-heading\">Hashset<\/h4>\n\n\n\n<p><code>HashSet&lt;T&gt;<\/code>\u662f\u4ee5\u54c8\u5e0c\u56fe\u5b9e\u73b0\u7684<strong>\u65e0\u5e8f\u4e0d\u5141\u8bb8\u91cd\u590d<\/strong>\u7684\u54c8\u5e0c\u96c6\u5408\uff0c\u53ea\u5173\u6ce8<code>key<\/code>\u7684\u952e\u800c\u4e0d\u5173\u5fc3<code>value<\/code>\u503c\u4e3a<code>()<\/code>\u3002\u53ea\u5b58\u201c\u552f\u4e00\u5143\u7d20\u201d\uff0c\u4e0d\u5b58\u5728\u91cd\u590d\uff0c\u5e76\u4e14\u67e5\u627e\u5341\u5206\u5feb(\u5e73\u5747 O(1))<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u6f14\u793a\u4ee3\u7801<\/summary>\n<pre class=\"wp-block-code\"><code>use std::collections::HashSet;\nfn main() {\n    let many_numbers = vec!&#91;\n        37, 3, 25, 11, 27, 3, 37, 21, 36, 19, 37, 30, 48, 28, 16, 33, 2,\n        10, 1, 12, 38, 35, 30, 21,\n        20, 38, 16, 48, 39, 31, 41, 32, 50, 7, 15, 1, 20, 3, 33, 12, 1, 11,\n        34, 38, 49, 1, 27, 9,\n        46, 33,\n    ];\n    println!(\"How many numbers in the Vec? {}\", many_numbers.len());\n    let mut number_hashset = HashSet::new();\n    for number in many_numbers {\n        number_hashset.insert(number);\n    }\n\n    let hashset_length = number_hashset.len();\n    println!(\n        \"There are {hashset_length} unique numbers, so we are missing {}.\",\n        50 - hashset_length\n    );\n    println!(\"It does not contain: \");\n    for number in 0..=50 {\n        if number_hashset.get(&amp;number).is_none() {\n            print!(\"{number} \");\n        }\n    }\n}\n\n\/*\nHow many numbers in the Vec? 50\nThere are 31 unique numbers, so we are missing 19.\nIt does not contain: \n0 4 5 6 8 13 14 17 18 22 23 24 26 29 40 42 43 44 45 47\n*\/<\/code><\/pre>\n<\/details>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/rustycab.github.io\/LearnRustEasy\/chapter_3\/chapter_3_15_6.html#3156-btreemapb-\u6811\u6620\u5c04\">BTreeMap  B\u6811\u6620\u5c04<\/a><\/h3>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use std::collections::BTreeMap;\n\nstruct City {\nname: String,\npopulation: BTreeMap&lt;i32, i32&gt;,\n}\n\nfn main() {\n    let mut tallinn = City {\n        name: \"Tallinn\".to_string(),\n        population: BTreeMap::new(),\n    };\n    tallinn.population.insert(2020, 437_619);\n    tallinn.population.insert(1372, 3_250);\n    tallinn.population.insert(1851, 24_000);\n    for (year, population) in tallinn.population {\n        println!(\"In {year}, Tallinn had a population of {population}.\");\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">BTreeSet<\/h4>\n\n\n\n<p><code>BTreeSet&lt;T&gt;<\/code>\u662f\u57fa\u4e8eBTressMap\u7684\u6709\u5e8f\u4e0d\u5141\u8bb8\u91cd\u590d\u7684\u96c6\u5408\uff0c\u81ea\u52a8\u6392\u5e8f\u9ed8\u8ba4\u4ece<strong>\u5c0f\u5230\u5927<\/strong>  \u57fa\u4e8e<code>Ord trait<\/code>  \u65f6\u95f4\u590d\u6742\u5ea6<code>O(log n)<\/code>  \u652f\u6301<strong>\u8303\u56f4\u67e5\u8be2<\/strong>\uff0c\u548cHashSet\u5f88\u76f8\u4f3c\u3002<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u6f14\u793a\u4ee3\u7801<\/summary>\n<pre class=\"wp-block-code\"><code>use std::collections::BTreeSet;\nfn main() {\n    let many_numbers = vec!&#91;37, 3, 25, 11, 27, 3, 37, 21, 36, 19, 37, 30, 48,\n        28, 16, 33, 2, 10, 1, 12, 38, 35, 30, 21, 20, 38, 16, 48, 39, 31, 41,\n        32, 50, 7, 15, 1, 20, 3, 33, 12, 1, 11, 34, 38, 49, 1, 27, 9, 46, 33];\n    let mut current_number = i32::MIN;\n    let mut number_set = BTreeSet::new();\n    for number in many_numbers {\n        number_set.insert(number);\n    }\n    for number in number_set {\n        if number &lt; current_number {\n            println!(\"This will never happen\");\n        }\n        current_number = number;\n    }\n}\n\/*\n\u4e0d\u4f1a\u6253\u5370\u4efb\u4f55\u4e1c\u897f\uff0c\u56e0\u4e3a\u6bcf\u4e00\u4e2a\u6570\u5b57\u90fd\u6bd4\u524d\u4e00\u4e2a\u6570\u5b57\u5927\n*\/<\/code><\/pre>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">BinaryHeap \u4e8c\u53c9\u5806<\/h3>\n\n\n\n<p>\u4ece\u4e8c\u53c9\u5806\u7684\u7ed3\u6784\u8bf4\u8d77\uff0c\u5b83\u662f\u4e00\u68f5\u4e8c\u53c9\u6811\uff0c\u5e76\u4e14\u662f\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u6bcf\u4e2a\u7ed3\u70b9\u4e2d\u5b58\u6709\u4e00\u4e2a\u5143\u7d20\uff08\u6216\u8005\u8bf4\uff0c\u6709\u4e2a\u6743\u503c\uff09\uff0e<\/p>\n\n\n\n<p>\u5806\u6027\u8d28\uff1a\u7236\u4eb2\u7684\u6743\u503c\u4e0d\u5c0f\u4e8e\u513f\u5b50\u7684\u6743\u503c\uff08\u5927\u6839\u5806\uff09\uff0e\u540c\u6837\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u5c0f\u6839\u5806\uff0e\u672c\u6587\u4ee5\u5927\u6839\u5806\u4e3a\u4f8b\uff0e<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-medium-font-size\"><strong>\u5927\u6839\u5806:<\/strong>&nbsp;\u7236\u7ed3\u70b9\u7684\u503c \u5927\u4e8e\u6216\u7b49\u4e8e \u5176\u5b50\u7ed3\u70b9\u7684\u503c<br><strong>\u5c0f\u6839\u5806:<\/strong>&nbsp;\u7236\u7ed3\u70b9\u7684\u503c \u5c0f\u4e8e\u6216\u7b49\u4e8e \u5176\u5b50\u7ed3\u70b9\u7684\u503c<\/p>\n<\/blockquote>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/oi-wiki.org\/ds\/images\/binary_heap_insert.svg\" alt=\"\" style=\"width:415px;height:auto\"\/><\/figure>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u5927\u6839\u5806 \u793a\u4f8b\u4ee3\u7801<\/summary>\n<pre class=\"wp-block-code\"><code>use std::collections::BinaryHeap;\nfn main() {\n    let many_numbers = vec!&#91;0, 5, 10, 15, 20, 25, 30];\n    let mut heap = BinaryHeap::new();\n    for num in many_numbers {\n        heap.push(num);\n    }\n    println!(\"First item is largest, others are out of order: {heap:?}\");\n    while let Some(num) = heap.pop() {\n        println!(\"Popped off {num}. Remaining numbers are: {heap:?}\");\n    }\n}<\/code><\/pre>\n<\/details>\n\n\n\n<h3 class=\"wp-block-heading\">VecDeque \u53cc\u5411\u961f\u5217<\/h3>\n\n\n\n<p><a href=\"https:\/\/doc.rust-lang.org\/std\/collections\/struct.VecDeque.html\">\u4f7f\u7528\u53ef\u589e\u957f\u7684\u73af\u5f62\u7f13\u51b2\u533a\u5b9e\u73b0\u7684\u53cc\u7aef\u961f\u5217<\/a>\u00a0<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>\u60a8\u9700\u8981\u4e00\u4e2a\u652f\u6301\u5728\u5e8f\u5217\u4e24\u7aef\u9ad8\u6548\u63d2\u5165\u7684\u00a0<a href=\"https:\/\/doc.rust-lang.org\/std\/vec\/struct.Vec.html\">Vec<\/a>\u3002<\/li>\n\n\n\n<li>\u60a8\u9700\u8981\u4e00\u4e2a\u961f\u5217\u3002<\/li>\n\n\n\n<li>\u60a8\u9700\u8981\u4e00\u4e2a\u53cc\u7aef\u961f\u5217 \uff08deque\uff09\u3002\u00a0<\/li>\n<\/ul>\n<cite>\u5728\u4e0a\u60c5\u51b5\u4f7f\u7528<\/cite><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\uff1f\u8fd0\u7b97\u7b26<\/h2>\n\n\n\n<p>\uff1f\u662f\u4e00\u4e2a\u66f4\u52a0\u7b80\u77ed\u7684\u65b9\u5f0f\u5904\u7406Result\uff0c\u76f8\u6bd4\u4e8ematch\u548cif let\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u7ed3\u679c\u662fOk\uff0c\u8fd4\u56deResult\u4e2d\u7684\u5185\u5bb9<\/li>\n\n\n\n<li>\u5982\u679c\u7ed3\u679c\u662fErr\uff0c\u5219\u76f4\u63a5\u7565\u8fc7<\/li>\n<\/ul>\n<cite>\u4e5f\u53ef\u4ee5\u5904\u7406option\u4f46\u5927\u591a\u5904\u7406Result<\/cite><\/blockquote>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u6f14\u793a\u4ee3\u7801<\/summary>\n<pre class=\"wp-block-code\"><code>use std::num::ParseIntError;\n    fn parse_and_log_str(input: &amp;str) -> Result&lt;i32, ParseIntError> {\n        let parsed_number = input.parse::&lt;i32>()?;\n        println!(\"Number parsed successfully into {parsed_number}\");\n        Ok(parsed_number)\n}\n    fn main() {\n    let str_vec = vec!&#91;\"Seven\", \"8\", \"9.0\", \"nice\", \"6060\"];\n    for item in str_vec {\n        let parsed = parse_and_log_str(item);\n        println!(\"Result: {parsed:?}\");\n    }\n}<\/code><\/pre>\n<\/details>\n\n\n\n<p>\u65e2 ? \u53ef\u4ee5\u63d0\u524d\u8fd4\u56de\u51fd\u6570\u90a3\u4e48\u5728mian()\u51fd\u6570\u4e2d\u4e5f\u53ef\u662f\u5e26Err()\u8fd4\u56de\u7ed3\u675f\u800c\u4e0d\u62a5\u9519parse<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u6f14\u793a\u4ee3\u7801<\/summary>\n<pre class=\"wp-block-code\"><code>use std::num::ParseIntError;\nfn main() -> Result&lt;(), ParseIntError> {\n    for item in vec!&#91;\"89\", \"8\", \"9.0\", \"eleven\", \"6060\"] {\n        let parsed = item.parse::&lt;u32>()?;\n        println!(\"{parsed}\");\n    }\n    Ok(())\n}<\/code><\/pre>\n<\/details>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li><code>?<\/code>\u00a0\u662f\u65e9\u8fd4\u56de\u673a\u5236<\/li>\n\n\n\n<li>\u8fd4\u56de\u7684 Err \u662f\u00a0<code>ParseIntError<\/code>\u00a0\u7ed3\u6784\u4f53\uff0c\u5305\u542b\u5931\u8d25\u4fe1\u606f<\/li>\n\n\n\n<li>\u8fde\u7eed\u8c03\u7528\u53ea\u8981\u9519\u8bef\u7c7b\u578b\u4e00\u81f4\uff0c\u6574\u4e2a\u94fe\u6761\u90fd\u80fd\u7528\u00a0<code>?<\/code>\u00a0\u7b80\u6d01\u8868\u8fbe<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u6f14\u793a\u4ee3\u7801<\/summary>\n<pre class=\"wp-block-code\" style=\"border-style:none;border-width:0px;margin-top:0;margin-bottom:0\"><code>use std::num::ParseIntError;\nfn parse_str(input: &amp;str) -> Result&lt;i32, ParseIntError> {\nlet parsed_number = input\n    .parse::&lt;u16>()?\n    .to_string()\n    .parse::&lt;u32>()?\n    .to_string()\n    .parse::&lt;i32>()?;\n    println!(\"Number parsed successfully into {parsed_number}\");\n\n    Ok(parsed_number)\n}\nfn main() {\n    let str_vec = vec!&#91;\"Seven\", \"8\", \"9.0\", \"nice\", \"6060\"];\n    for item in str_vec {\n    let parsed = parse_str(item);\n    println!(\"{parsed:?}\");\n    }\n}<\/code><\/pre>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f55\u65f6\u4f7f\u7528panic\u548cunwrap<\/h2>\n\n\n\n<p>Rust \u8bed\u8a00\u63d0\u4f9b\u4e86\u4e24\u4e2a\u4e92\u8865\u7684\u7cfb\u7edf\uff0c\u7528\u4e8e\u6784\u5efa\/\u8868\u793a\u3001\u62a5\u544a\u3001\u4f20\u64ad\u3001\u54cd\u5e94\u548c\u4e22\u5f03\u9519\u8bef\u3002\u8fd9\u4e9b\u804c\u8d23\u7edf\u79f0\u4e3a\u201c\u9519\u8bef\u5904\u7406\u201d\u3002\u00a0<code>panic!<\/code>\u00a0\u548c\u00a0<code>Result<\/code>\u00a0\u5728\u5b83\u4eec\u5404\u81ea\u9519\u8bef\u5904\u7406\u7cfb\u7edf\u4e2d\u7684\u4e3b\u8981\u63a5\u53e3\u65b9\u9762\u662f\u76f8\u4f3c\u7684\uff1b\u7136\u800c\uff0c\u8fd9\u4e9b\u63a5\u53e3\u9644\u52a0\u7ed9\u5b83\u4eec\u7684\u9519\u8bef\u7684\u610f\u4e49\u4ee5\u53ca\u5b83\u4eec\u5728\u5176\u5404\u81ea\u9519\u8bef\u5904\u7406\u7cfb\u7edf\u4e2d\u6240\u627f\u62c5\u7684\u804c\u8d23\u6709\u6240\u4e0d\u540c<\/p>\n\n\n\n<p><code>panic! <\/code>\u5b8f\u7528\u4e8e\u6784\u5efa\u8868\u793a\u7a0b\u5e8f\u4e2d\u5df2\u68c0\u6d4b\u5230\u9519\u8bef\u7684\u9519\u8bef\u3002\u4f7f\u7528 <code>panic! <\/code>\u4f60\u63d0\u4f9b\u63cf\u8ff0\u9519\u8bef\u7684\u6587\u672c\uff0c\u7136\u540e\u8bed\u8a00\u4f1a\u4f7f\u7528\u8be5\u6587\u672c\u6784\u5efa\u9519\u8bef\uff0c\u62a5\u544a\u5b83\uff0c\u5e76\u4e3a\u4f60\u4f20\u64ad\u5b83\u3002<\/p>\n\n\n\n<p><code>Result<\/code> \u53e6\u4e00\u65b9\u9762\u7528\u4e8e\u5c01\u88c5\u5176\u4ed6\u7c7b\u578b\uff0c\u8fd9\u4e9b\u7c7b\u578b\u4ee3\u8868\u67d0\u79cd\u8ba1\u7b97\u7684\u6210\u529f\u7ed3\u679c <code>Ok(T)<\/code> \uff0c\u6216\u8868\u793a\u8be5\u8ba1\u7b97\u9884\u671f\u8fd0\u884c\u65f6\u5931\u8d25\u6a21\u5f0f\u7684\u9519\u8bef\u7c7b\u578b <code>Err(E)<\/code> \u3002 <code>Result<\/code> \u4e0e\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b\u4e00\u8d77\u4f7f\u7528\uff0c\u8fd9\u4e9b\u7c7b\u578b\u4ee3\u8868\u5173\u8054\u8ba1\u7b97\u53ef\u80fd\u9047\u5230\u7684\u9884\u671f\u8fd0\u884c\u65f6\u5931\u8d25\u6a21\u5f0f\u3002 <code>Result<\/code> \u5fc5\u987b\u624b\u52a8\u4f20\u64ad\uff0c\u901a\u5e38\u501f\u52a9<code> ? <\/code>\u64cd\u4f5c\u7b26\u548c <code>Try<\/code> \u7279\u6027\uff0c\u5e76\u4e14\u5fc5\u987b\u624b\u52a8\u62a5\u544a\uff0c\u901a\u5e38\u501f\u52a9<code> Error <\/code>\u7279\u6027\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/doc.rust-lang.org\/std\/result\/index.html\" target=\"_blank\" rel=\"noreferrer noopener\">std::result<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">panic<\/h3>\n\n\n\n<p><code><strong>panic!<\/strong>\u00a0<\/code>\u8fd9\u662f\u4e00\u79cd\u5bf9\u9519\u8bef\u6761\u4ef6\u7684\u54cd\u5e94\uff0c\u8fd9\u79cd\u9519\u8bef\u901a\u5e38\u5728\u9047\u5230\u5b83\u7684\u4e0a\u4e0b\u6587\u4e2d\u662f\u4e0d\u53ef\u6062\u590d\u7684\u3002\u5b83\u4f1a\u7acb\u5373\u7ec8\u6b62\u5f53\u524d\u7ebf\u7a0b\uff0c\u5e76\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6253\u5370\u9519\u8bef\u4fe1\u606f\u53ca\u53d1\u751f\u4f4d\u7f6e\u3002<code>panic!\u00a0<\/code>\u5e38\u7528\u4e8e\u7a0b\u5e8f\u4e2d\u68c0\u6d4b\u5230<strong>\u903b\u8f91\u9519\u8bef\u6216 bug<\/strong>\u65f6\uff0c\u4f8b\u5982\u6570\u7ec4\u8d8a\u754c\u3001\u9664\u4ee5\u96f6\u7b49\u3002<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u53ef\u4ee5\u643a\u5e26\u4fe1\u606f\u8fd4\u56de\u5e76\u7ec8\u6b62\u7ebf\u7a0b<\/summary>\n<pre class=\"wp-block-code\"><code>fn main() {\n    panic!(\"Time to panic!\");\n}<\/code><\/pre>\n<\/details>\n\n\n\n<p>\u6709\u4e09\u79cd\u51fd\u6570\u7c7b\u4f3c<code>panic!<\/code> \u53ef\u4ee5\u7528\u6765\u6d4b\u8bd5<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li><code>assert!(expression)<\/code>\u5982\u679c\u62ec\u53f7\u5185\u7684\u8868\u8fbe\u5f0f\u7684\u503c\u662f\u00a0<code>false<\/code>\u00a0\u5219 panic<\/li>\n\n\n\n<li><code>assert_eq!(left, right)<\/code>\u5982\u679c\u62ec\u53f7\u4e24\u8fb9\u6570\u503c\u4e0d\u7b49\u5219panic<\/li>\n\n\n\n<li><code>assert_ne!()<\/code>\u5982\u679c\u62ec\u53f7\u4e24\u8fb9\u6570\u503c\u76f8\u7b49\u5219panic<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">unwrap<\/h3>\n\n\n\n<p>\u5728\u9519\u8bef\u5904\u7406\u4e2d\u4f7f\u7528<code>unwrap()<\/code>\u5f97\u4e0d\u5230\u9519\u8bef\u4fe1\u606f\u56e0\u6b64\u5c1d\u8bd5\u4f7f\u7528<code>expect()<\/code>\u53ef\u4ee5\u5f97\u5230\u9519\u8bef\u4fe1\u606f\u5229\u4e8e\u51fa\u95ee\u9898\u65f6\u7684\u8bc6\u8bfb<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u4f7f\u7528<code>unwrap()<\/code><\/summary>\n<pre class=\"wp-block-code\"><code>fn get_fourth(input: &amp;Vec&lt;i32>) -> i32 {\n    let fourth = input.get(3).unwrap();\n    *fourth\n}\nfn main() {\n    let my_vec = vec!&#91;9, 0, 10, 8, 7];\n    let fourth = get_fourth(&amp;my_vec);\n    println!(\"The fourth item is: {}\", fourth);\n}\n\n\/*\nthread 'main' panicked at 'called Option::unwrap() on a None value', src\\main.rs:7:18.\n*\/<\/code><\/pre>\n<\/details>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u4f7f\u7528<code>expect()<\/code><\/summary>\n<pre class=\"wp-block-code\"><code>fn get_fourth(input: &amp;Vec&lt;i32>) -> i32 {\n    let fourth = input.get(3).expect(\"Input vector needs at least 4 items\");\n    *fourth\n}\nfn main() {\n    let my_vec = vec!&#91;9, 0, 10, 8, 7];\n    let fourth = get_fourth(&amp;my_vec);\n    println!(\"The fourth item is: {}\", fourth);\n}\n\n\/*\nthread 'main' panicked at 'Input vector needs at least 4 items',\nsrc\\main.rs:7:18\n*\/<\/code><\/pre>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u4e8e\u6709\u952e\u548c\u503c\u7684\u6570\u636e\uff0c\u901a\u5e38\u4f7f\u7528<code>Hashmap<\/code>\u3002\u5982\u679c\u9700\u8981\u6709\u5e8f\u6392\u5e8f\u4f7f\u7528<code>BTreemap<\/code><\/li>\n\n\n\n<li>\u5982\u679c\u53ea\u60f3\u9a8c\u8bc1\u67d0\u79cd\u4e1c\u897f\u662f\u5426\u5b58\u5728\uff0c\u901a\u5e38\u4f7f\u7528<code>HashSet<\/code>\u3002\u5982\u679c\u9700\u8981\u6709\u5e8f\u96c6\u5408\u4f7f\u7528<code>BTreeSet<\/code>\u3002<\/li>\n\n\n\n<li><code>VecDeque<\/code>\u6bd4<code>Vec<\/code>\u6162\uff0c\u9664\u975e\u4f60\u9700\u8981\u540c\u65f6\u5728\u524d\u9762\u548c\u540e\u9762\u64cd\u4f5c\u6570\u636e\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c<code>VecDeque<\/code>\u8981\u5feb\u5f97\u591a\u3002<\/li>\n\n\n\n<li><code>BTreeSet<\/code>\u603b\u662f\u5728\u524d\u9762\u5177\u6709\u6700\u5927\u7684\u503c\u3002\u5176\u4f59\u4e00\u5207\u90fd\u6ca1\u6709\u5206\u7c7b\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528<code>\uff1f<\/code>\u5f88\u65b9\u4fbf\uff0c\u5b83\u4f1a\u81ea\u52a8\u4ece\u8fd4\u56de\u7684\u7ed3\u679c\u4e2d\u63d0\u53d6<code>OK<\/code>\u503c\u3002\u5982\u679c\u503c\u662f<code>Err<\/code>\uff0c\u5c06\u63d0\u524d\u9000\u51fa\u51fd\u6570\u5e76\u8fd4\u56de\u9519\u8bef\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528<code>Result<\/code>\u548c<code>Option<\/code>\uff0c\u60a8\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f<code>panic<\/code>\uff0c\u4f46\u6709\u65f6<code>panic<\/code>\u662f\u6709\u610f\u4e49\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7ae0\u6897\u6982<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.shufue.top\/index.php?rest_route=\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.shufue.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.shufue.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.shufue.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.shufue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=54"}],"version-history":[{"count":8,"href":"https:\/\/blog.shufue.top\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/blog.shufue.top\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/blog.shufue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.shufue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.shufue.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}