{"id":254,"date":"2022-01-10T23:18:40","date_gmt":"2022-01-10T15:18:40","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=254"},"modified":"2022-03-30T21:02:37","modified_gmt":"2022-03-30T13:02:37","slug":"web-rust%e5%9f%ba%e7%a1%80%e4%b8%80-2","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2022\/01\/10\/web-rust%e5%9f%ba%e7%a1%80%e4%b8%80-2\/","title":{"rendered":"Web Rust\u57fa\u7840\u4e8c"},"content":{"rendered":"<h1>\u5faa\u73af-\u6d41\u7a0b\u63a7\u5236<\/h1>\n<p>\u5faa\u73af\u4e0e\u5176\u4ed6\u8bed\u8a00\u5927\u81f4\u76f8\u540c\uff1a<\/p>\n<pre><code class=\"language-rust\">\/\/\u4f5c\u7528\u57df\u5757\n    let a = {\n        println!(&quot;ABC&quot;); \/\/ \u8f93\u51fa ABC\n        &quot;DEF&quot; \/\/ \u4f5c\u4e3a\u8fd9\u4e2a\u82b1\u62ec\u53f7\u5757\u7684\u503c\n    }; \/\/ \u5206\u53f7\u4e0d\u53ef\u7701\u7565\n    println!(&quot;{}&quot;, a);\n\n    \/\/if else\n    let a1 = 1;\n    let b1 = 2;\n    let c1 = if a1 &lt; b1 {\n        println!(&quot;true&quot;);\n        &quot;ABC&quot;\n    } else {\n        println!(&quot;false&quot;);\n        &quot;DEF&quot;\n    };\n    println!(&quot;{}&quot;, c1);\n    let a2 = 1;\n    let b2 = 2;\n    if a2 &lt; b2 {\n        println!(&quot;if true 1&quot;);\n    } else if a2 == b2 {\n        println!(&quot;if true 2&quot;);\n    }\n\n    \/\/match(\u7c7b\u4f3cswitch)\n    let week = &quot;mon&quot;;\n    let a3 = match week {\n        &quot;sun&quot; =&gt; &quot;weekend&quot;,\n        &quot;mon&quot; | &quot;tue&quot; | &quot;wed&quot; | &quot;thu&quot; | &quot;fri&quot; =&gt; &quot;weekday&quot;,\n        &quot;sat&quot; =&gt; &quot;weekend&quot;,\n        _ =&gt; &quot;illegal&quot;,\n    }; \/\/\u5728\u8868\u8fbe\u5f0f\u503c\u4e3a\u5b57\u7b26\u4e32\u6216\u8005\u6570\u503c\u65f6\uff0c\u672b\u5c3e\u9700\u8981\u6709\u9ed8\u8ba4\u5206\u652f _ \u3002\n    println!(&quot;{}&quot;, a3);\n    \/\/\u9ed8\u8ba4\u5206\u652f\u4e5f\u53ef\u4ee5\u7528\u4e00\u4e2a\u53d8\u91cf\u4ee3\u66ff\uff0c\u8fd9\u6837\uff0c\u53d8\u91cf\u4f1a\u88ab\u8d4b\u4e88\u8868\u8fbe\u5f0f\u503c\n    let day = &quot;xxx&quot;;\n    match day {\n        &quot;sun&quot; | &quot;sat&quot; =&gt; {\n            println!(&quot;weekend&quot;);\n        }\n        &quot;mon&quot; | &quot;tue&quot; | &quot;wed&quot; | &quot;thu&quot; | &quot;fri&quot; =&gt; {\n            println!(&quot;weekday&quot;);\n        }\n        other =&gt; {\n            println!(&quot;{} is illegal&quot;, other);\n        }\n    }; \/\/\u5982\u679c\u8868\u8fbe\u5f0f\u7684\u503c\u4e3a bool \u7c7b\u578b\uff0c\u679a\u4e3e\u5b8c true \u548c false \u4e24\u4e2a\u5206\u652f\uff0c\u4e0d\u9700\u8981\u9ed8\u8ba4\u5206\u652f\n\n    \/\/while\n    let mut num1 = 0;\n    while num1 &lt; 10 {\n        println!(&quot;{}&quot;, num1);\n        num1 += 1;\n    }\n\n    \/\/loop\n    let mut num2 = 0;\n    loop {\n        println!(&quot;{}&quot;, num2);\n        num2 += 1;\n        if num2 &lt; 10 {\n            continue;\n        }\n        break;\n    }\n    \/\/loop\u8868\u8fbe\u5f0f\n    let mut num3 = 0;\n    let num4 = loop {\n        println!(&quot;{}&quot;, num3);\n        num3 += 1;\n        if num3 == 10 {\n            break num3;\n        }\n    };\n    println!(&quot;{}&quot;, num4);\n\n    \/\/for in\u5faa\u73af\n    let vec5 = vec![1, 2, 3];\n    for v in &amp;vec5 {\n        println!(&quot;{}&quot;, v);\n    }\n    for v1 in 10..20 {\n        println!(&quot;{}&quot;, v1);\n    }<\/code><\/pre>\n<h1>\u6570\u636e\u548c\u5904\u7406<\/h1>\n<h2>struct<\/h2>\n<p>struct \u53ef\u4ee5\u7528\u4e8e\u5c06\u51e0\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u5b57\u6bb5\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u7c7b\u578b\uff08\u4e0e C \u8bed\u8a00\u7684 struct \u7c7b\u4f3c\uff09:<\/p>\n<pre><code class=\"language-rust\">\/\/...\nstruct Size {\n    width: f32,\n    height: f32,\n}\n\n\/\/ struct Size(f32, f32);\/\/\u533f\u540d\n\n\/\/...\nlet my_size = Size {\n        width: 2.0,\n        height: 3.4,\n    };\nlet area = my_size.width * my_size.height;\nprintln!(&quot;{}&quot;, area); <\/code><\/pre>\n<p>\u53d6\u5b50\u5b57\u6bb5\u53ef\u4ee5\u53ea\u53d6\u5f15\u7528\uff1a<\/p>\n<pre><code class=\"language-rust\">\/\/...\nstruct Product {\n    name: String,\n    price: u32,\n}\n\n\/\/...\nlet my_product = Product {\n        name: format!(&quot;Rust Book&quot;),\n        price: 30,\n    };\nlet name: &amp;str = &amp;my_product.name;\nlet price = my_product.price;\nprintln!(&quot;{}: {}&quot;, name, price);<\/code><\/pre>\n<p>\u5206\u6563\u8d4b\u503c\u5206\u4e3a\u53d6\u503c\u548c\u53d6\u5f15\u7528:<\/p>\n<pre><code class=\"language-rust\">let Product { name, price } = my_product;\nlet Product { name, price } = &amp;my_product;\nlet Product {\n    ref mut name,\n    ref price,\n} = my_product;\nlet Product { ref name, .. } = my_product;<\/code><\/pre>\n<h2>enum<\/h2>\n<p>enum \u4e0e\u5f88\u591a\u7f16\u7a0b\u8bed\u8a00\u7684\u679a\u4e3e\u7c7b\u578b\u4e0d\u540c\uff0c\u5b83\u7684\u6bcf\u4e2a\u679a\u4e3e\u9879\u53ef\u4ee5\u9644\u52a0\u4e0d\u540c\u7684\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-rust\">\/\/...\nenum Shape {\n    Rectangle {\n        \/\/ Rectangle \u9644\u6709 width \u548c height \u4e24\u4e2a\u5b57\u6bb5\n        width: f32,\n        height: f32,\n    },\n    Square(f32), \/\/ Square \u53ea\u9644\u6709\u4e00\u4e2a\u5b57\u6bb5\n    Round(f32),  \/\/ Round \u53ea\u9644\u6709\u4e00\u4e2a\u5b57\u6bb5\n    Point,       \/\/ Point \u6ca1\u9644\u5b57\u6bb5\n}\n\n\/\/...\nlet my_shape1 = Shape::Rectangle {\n        width: 30.0,\n        height: 20.0,\n    };\nlet area1 = match my_shape1 {\n    Shape::Rectangle { width, height } =&gt; width * height,\n    Shape::Square(len) =&gt; len * len,\n    Shape::Round(r) =&gt; r * r * std::f32::consts::PI,\n    Shape::Point =&gt; 0.,\n};\nprintln!(&quot;{}&quot;, area1);<\/code><\/pre>\n<p>\u5982\u679c\u53ea\u9700\u8981\u5224\u65ad\u5176\u4e2d\u4e00\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528 if let:<\/p>\n<pre><code class=\"language-rust\">if let Shape::Rectangle { width, height } = my_shape {\n    println!(&quot;Rectangle {}&quot;, width * height);\n}<\/code><\/pre>\n<p>match \u548c if let \u4e5f\u90fd\u53ef\u4ee5\u53ea\u53d6\u5f15\u7528\uff0c\u7c7b\u4f3c\u4e8e\u5206\u6563\u8d4b\u503c\u7684\u505a\u6cd5:<\/p>\n<pre><code class=\"language-rust\">match &amp;my_shape {\n    Shape::Rectangle { width, height } =&gt; {\n        \/\/ width \u548c height \u662f &amp;f32 \u7c7b\u578b\n    }\n    _ =&gt; {}\n}\nif let Shape::Rectangle { ref mut width, .. } = my_shape {\n    \/\/ width \u662f &amp;mut f32 \u7c7b\u578b\n}<\/code><\/pre>\n<h2>\u6570\u636e\u8868\u8fbe\u5f0f<\/h2>\n<p>\u8868\u8fbe\u73b0\u5b9e\u4e2d\u7684\u6570\u636e\u65f6\uff0c\u4e00\u822c\u662f struct \u548c enum \u7684\u7ed3\u5408\uff1astruct \u7528\u4e8e\u8868\u8fbe\u6570\u636e\u4e2d\u6c38\u8fdc\u5b58\u5728\u7684\u5b57\u6bb5\uff0c enum \u7528\u4e8e\u8868\u8fbe\u6709\u65f6\u5b58\u5728\u3001\u6709\u65f6\u4e0d\u5b58\u5728\u7684\u6570\u636e\u5b57\u6bb5\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5f53\u524d\u5929\u6c14\u60c5\u51b5\u7531\u6c14\u6e29\u548c\u5929\u6c14\u72b6\u51b5\u7ec4\u6210\uff0c\u5176\u4e2d\u5929\u6c14\u72b6\u51b5\u53ef\u4ee5\u662f\u6674\u6216\u8005\u96e8\uff0c\u5982\u679c\u662f\u96e8\uff0c\u5b83\u7684\u964d\u6c34\u6982\u7387\u53ef\u4ee5\u4e3a 0 \u81f3 100% \uff0c\u8fd9\u6837\u7684\u6570\u636e\u53ef\u4ee5\u4f7f\u7528 struct \u548c enum \u8868\u8fbe\u4e3a\uff1a<\/p>\n<pre><code class=\"language-rust\">struct CurrentWeather {\n    temperature: f32,\n    condition: WeatherCondition,\n}\nenum WeatherCondition {\n    Sunny,\n    Rainy { probability: f32 },\n}<\/code><\/pre>\n<h2>debug<\/h2>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c struct \u548c enum \u672c\u8eab\u662f\u4e0d\u80fd\u4f7f\u7528 println! \u548c dbg! \u6765\u8f93\u51fa\u7684\u3002\u52a0\u4e0a derive(Debug) \u53ef\u4ee5\u4f7f\u5f97\u5b83\u80fd\u591f\u88ab\u8c03\u8bd5\u8f93\u51fa\uff0c\u6b64\u65f6 println! \u4e2d\u9700\u8981\u4f7f\u7528 {:?} \u4f5c\u4e3a\u5360\u4f4d\u7b26:<\/p>\n<pre><code class=\"language-rust\">#[derive(Debug)]\nstruct CurrentWeather {\n    temperature: f32,\n    condition: WeatherCondition,\n}\n#[derive(Debug)]\nenum WeatherCondition {\n    Sunny,\n    Rainy { probability: f32 },\n}\nfn main() {\n    let current_weather = CurrentWeather {\n        temperature: 10.,\n        condition: WeatherCondition::Sunny,\n    };\n    println!(&quot;{:?}&quot;, current_weather);\n}<\/code><\/pre>\n<h2>\u76f8\u7b49\u5224\u5b9a<\/h2>\n<p>\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u6807\u8bb0\u662f derive(PartialEq) \uff0c\u5b83\u53ef\u4ee5\u4f7f\u5f97\u4e24\u4e2a\u540c\u4e00\u7c7b\u578b\u7684 struct \u6216 enum \u88ab\u4f7f\u7528 == \u6765\u6bd4\u8f83\u662f\u5426\u76f8\u7b49\uff08\u610f\u5473\u7740\u6240\u6709\u5b50\u5b57\u6bb5\u90fd\u5206\u522b\u76f8\u7b49\uff09:<\/p>\n<pre><code class=\"language-rust\">#[derive(Debug, PartialEq)]\nstruct CurrentWeather {\n    temperature: f32,\n    condition: WeatherCondition,\n}\n#[derive(Debug, PartialEq)]\nenum WeatherCondition {\n    Sunny,\n    Rainy { probability: f32 },\n}\nfn main() {\n    let a = CurrentWeather {\n        temperature: 10.,\n        condition: WeatherCondition::Sunny,\n    };\n    let b = CurrentWeather {\n        temperature: 10.,\n        condition: WeatherCondition::Sunny,\n    };\n    println!(&quot;{}&quot;, a == b);\n}\n``\n\n## type\n\n\u4f7f\u7528 type \u8bed\u53e5\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a struct \u6216 enum \u7684\u522b\u540d\uff0c\u6709\u65f6\u5019\u53ef\u4ee5\u7f29\u77ed\u4ee3\u7801\uff1a\n\n```rust\ntype CurWth = CurrentWeather;\ntype WthCond = WeatherCondition;\nfn main() {\n    let a = CurrentWeather {\n        temperature: 10.,\n        condition: WeatherCondition::Sunny,\n    };\n    let b = CurWth {\n        temperature: 10.,\n        condition: WthCond::Sunny,\n    };\n    println!(&quot;{}&quot;, a == b);\n}<\/code><\/pre>\n<p><em>\u6ce8\u610f\uff1a\u8fd9\u6837\u53ea\u662f\u751f\u6210\u4e86\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u522b\u540d\uff0c\u5e76\u6ca1\u6709\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b\uff0c\u56e0\u6b64\u53ea\u8981\u539f\u7c7b\u578b\u6709 derive(PartialEq)  \uff0c\u4f7f\u7528\u539f\u540d\u548c\u522b\u540d\u521b\u5efa\u7684\u6570\u636e\u662f\u53ef\u4ee5\u76f8\u4e92\u6bd4\u8f83\u7684\u3002\uff08\u5728\u5b9e\u9645\u4f7f\u7528\u65f6\uff0c\u5982\u679c\u9700\u8981\u907f\u514d\u5b83\u4eec\u88ab\u76f8\u4e92\u6bd4\u8f83\u6216\u8005\u6df7\u7528\uff0c\u8fd8\u662f\u9700\u8981\u4f7f\u7528 struct \u6765\u91cd\u65b0\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b\u3002\uff09<\/em><\/p>\n<h2>\u5e8f\u5217\u5316<\/h2>\n<p>\u5728\u5b9e\u9645\u4f7f\u7528\u65f6\uff0c\u6570\u636e\u5f80\u5f80\u9700\u8981\u7ecf\u8fc7\u5e8f\u5217\u5316\u624d\u80fd\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u6216\u4e0e\u5176\u4ed6\u8bed\u8a00\u7684\u4ee3\u7801\u4ea4\u4e92\u3002\u6b64\u65f6\u9700\u8981\u7528\u5230\u5e38\u7528\u7684\u5e8f\u5217\u5316\u6846\u67b6 serde \u3002\u4ee5 JSON \u683c\u5f0f\u7684\u5e8f\u5217\u5316\u4e3a\u4f8b\uff0c\u9996\u5148\u5728 Cargo.toml \u7684 [dependencies] \u4e2d\u5f15\u5165\uff1a<\/p>\n<pre><code class=\"language-rust\">[dependencies]\nserde = { version = &quot;1&quot;, features = [&quot;derive&quot;] }\nserde_json = &quot;1&quot;<\/code><\/pre>\n<p>\u7136\u540e\u5bf9\u9700\u8981\u5e8f\u5217\u5316\u7684 struct \u548c enum \u6dfb\u52a0 derive(Serialize) \u3001 derive(Deserialize) \uff1a<\/p>\n<pre><code class=\"language-rust\">use serde::{Serialize, Deserialize};\n#[derive(Debug, Serialize, Deserialize)]\nstruct CurrentWeather {\n    temperature: f32,\n    condition: WeatherCondition,\n}\n#[derive(Debug, Serialize, Deserialize)]\nenum WeatherCondition {\n    Sunny,\n    Rainy { probability: f32 },\n}\nfn main() {\n    let cw = CurrentWeather {\n        temperature: 10.,\n        condition: WeatherCondition::Sunny,\n    };\n    let cw_json = serde_json::to_string(&amp;cw).unwrap();\n    println!(&quot;{}&quot;, cw_json);\n    let cw: CurrentWeather = serde_json::from_str(&amp;cw_json).unwrap();\n    println!(&quot;{:?}&quot;, cw);\n}<\/code><\/pre>\n<p>code enjoy! \ud83d\udc1c\ud83d\udc1c\ud83d\udc1c<\/p>\n<p>\u4f5c\u8005\uff1aindeex  <\/p>\n<p>\u94fe\u63a5\uff1a<a href=\"https:\/\/indeex.club\/\">https:\/\/indeex.club<\/a>  <\/p>\n<p>\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5faa\u73af-\u6d41\u7a0b\u63a7\u5236 \u5faa\u73af\u4e0e\u5176\u4ed6\u8bed\u8a00\u5927\u81f4\u76f8\u540c\uff1a \/\/\u4f5c\u7528\u57df\u5757 let a = { println!(&#038;qu<a href=\"https:\/\/blog.indeex.club\/index.php\/2022\/01\/10\/web-rust%e5%9f%ba%e7%a1%80%e4%b8%80-2\/\" class=\"read-more\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[14],"_links":{"self":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/254"}],"collection":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/comments?post=254"}],"version-history":[{"count":4,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":275,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/254\/revisions\/275"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}