{"id":256,"date":"2022-02-02T21:59:05","date_gmt":"2022-02-02T13:59:05","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=256"},"modified":"2022-03-30T21:04:25","modified_gmt":"2022-03-30T13:04:25","slug":"web-rust%e5%9f%ba%e7%a1%80%e4%b8%89","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2022\/02\/02\/web-rust%e5%9f%ba%e7%a1%80%e4%b8%89\/","title":{"rendered":"Web Rust\u57fa\u7840\u4e09"},"content":{"rendered":"<h1>\u6a21\u5757\u5316<\/h1>\n<h2>\u51fd\u6570<\/h2>\n<p>\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u80fd\u7701\u7565\uff1a<\/p>\n<pre><code class=\"language-rust\">fn a_plus_b(a: i32, b: i32) -&gt; i32 {\n    return a + b;\n}\n\nfn main() {\n    println!(&quot;{}&quot;, a_plus_b(1, 2));\n}<\/code><\/pre>\n<p>\u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u50cf\u7528 return \u8fd4\u56de\u3002\u66f4\u5e38\u89c1\u7684\u662f\uff0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5982\u679c\u4e0d\u4ee5\u5206\u53f7\u7ed3\u5c3e\uff0c\u5b83\u7684\u503c\u5c31\u662f\u8fd9\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-rust\">fn a_plus_b(a: i32, b: i32) -&gt; i32 {\n    a + b\n}<\/code><\/pre>\n<p>\u7ed9\u51fd\u6570\u4f20\u53c2\u65f6\uff0c\u5bf9\u4e8e\u5b57\u7b26\u4e32\u6216\u8005\u590d\u6742 struct \uff0c\u4e3a\u4e86\u907f\u514d\u590d\u5236\uff0c\u901a\u5e38\u5e94\u4f20\u9012\u5f15\u7528:<\/p>\n<pre><code class=\"language-rust\">fn concat_str(a: &amp;str, b: &amp;str) -&gt; String {\n    format!(&quot;{}{}&quot;, a, b)\n}F\nfn main() {\n    let a: String = format!(&quot;A&quot;);\n    let b: String = format!(&quot;BC&quot;);\n    println!(&quot;{}&quot;, concat_str(&amp;a, &amp;b));\n}<\/code><\/pre>\n<h2>\u5173\u8054\u51fd\u6570<\/h2>\n<p>\u51fd\u6570\u53ef\u4ee5\u5173\u8054\u5728 struct \u6216 enum \u4e0a\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u50cf\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8bed\u8a00\u7684\u5bf9\u8c61\u65b9\u6cd5\u90a3\u6837\u8c03\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-rust\">struct Rectangle {\n    width: f32,\n    height: f32,\n}\n\n\/\/ Rectangle \u7684\u5173\u8054\u51fd\u6570\u5217\u8868\nimpl Rectangle {\n    fn area(&amp;self) -&gt; f32 { \/\/ &amp;self \u8868\u793a\u53d6 Rectangle \u7684\u4e0d\u53ef\u53d8\u5f15\u7528\n        self.width * self.height\n    }\n\n    fn extend(\n        &amp;mut self, \/\/ &amp;mut self \u8868\u793a\u53d6 Rectangle \u7684\u53ef\u53d8\u5f15\u7528\n        extend_width: f32,\n        extend_height: f32,\n    ) {\n        self.width += extend_width;\n        self.height += extend_height;\n    }\n\n    fn new( \/\/ \u4e5f\u53ef\u4ee5\u4e0d\u53d6 Rectangle \u7684\u5f15\u7528\n        initial_width: f32,\n        initial_height: f32,\n    ) -&gt; Self {\n        Self { \/\/ Self \u6307\u4ee3 Rectangle \uff08\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5199\u6210 Rectangle \uff09\n            width: initial_width,\n            height: initial_height,\n        }\n    }\n}\n\nfn main() {\n    \/\/ \u5173\u8054\u51fd\u6570\u53ef\u4ee5\u7528 :: \u5f62\u5f0f\u8c03\u7528\n    let mut rect = Rectangle::new(2.0, 3.5);\n    \/\/ \u5982\u679c\u5173\u8054\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f self \u7684\u8bdd\uff0c\u8fd8\u53ef\u4ee5\u8fd9\u6837\u8c03\u7528\n    rect.extend(1.0, -2.0);\n    println!(&quot;{}&quot;, rect.area()); \/\/ \u8f93\u51fa 4.5\n}<\/code><\/pre>\n<p>\u4e00\u822c\u7684\uff0c\u5f88\u591a struct \u90fd\u6709\u4e2a\u547d\u540d\u4e3a new \u7684\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de Self \uff0c\u7c7b\u4f3c\u4e8e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u6784\u9020\u5668\uff1b\u5982\u679c struct \u4e0d\u5e94\u8be5\u88ab\u76f4\u63a5\u6784\u9020\uff0c\u90a3\u5b83\u5c31\u6ca1\u6709 new \u65b9\u6cd5\u3002<\/p>\n<h2>\u5b50\u6a21\u5757<\/h2>\n<p>\u5f53\u9879\u76ee\u8f83\u5927\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u5c06\u90e8\u5206 struct \u3001 enum \u548c\u51fd\u6570\u7b49\u62c6\u5206\u5230\u5b50\u6a21\u5757\u4e2d\u3002<\/p>\n<p>\u5b50\u6a21\u5757\u5199\u6cd5\u6709\u5f88\u591a\u79cd\u3002\u5b83\u53ef\u4ee5\u76f4\u63a5\u7528 mod &#8230; { } \u5b9a\u4e49\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-rust\">\/\/ \u5b9a\u4e49\u4e00\u4e2a\u5b50\u6a21\u5757\nmod utils {\n    pub fn a_plus_b(a: i32, b: i32) -&gt; i32 { \/\/ pub \u8868\u793a\u53ef\u4ee5\u5728\u5b50\u6a21\u5757\u5916\u8bbf\u95ee\n        a + b\n    }\n}\n\nfn main() {\n    let sum = utils::a_plus_b(1, 2); \/\/ \u4f7f\u7528 :: \u8bbf\u95ee\u5176\u4e2d\u7684\u51fd\u6570\n    println!(&quot;{}&quot;, sum); \/\/ \u8f93\u51fa 3\n}<\/code><\/pre>\n<p>\u66f4\u5e38\u89c1\u7684\u505a\u6cd5\u662f\uff0c\u5c06\u5b50\u6a21\u5757\u7684\u5185\u5bb9\u5355\u72ec\u5199\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6\u91cc\u3002\u4f8b\u5982\uff0c\u4e0e main.rs \u540c\u4e00\u4e2a\u76ee\u5f55\u4e2d\u8fd8\u6709\u4e00\u4e2a\u6587\u4ef6 utils.rs \u6216\u5728 utils \u5b50\u76ee\u5f55\u4e0b\u6709 mod.rs\uff0c\u5185\u5bb9\u662f\uff1a<\/p>\n<pre><code class=\"language-rust\">\/\/ utils.rs \u6216 utils\/mod.rs\npub fn a_plus_b(a: i32, b: i32) -&gt; i32 {\n    a + b\n}<\/code><\/pre>\n<p>\u5728 main.rs \u4e2d\u5c31\u53ef\u4ee5\u5f15\u5165\uff1a<\/p>\n<pre><code class=\"language-rust\">\/\/ \u5c06 utils \u7684\u5185\u5bb9\u4f5c\u4e3a\u5b50\u6a21\u5757\u5f15\u5165\nmod utils;\n\nfn main() {\n    let sum = utils::a_plus_b(1, 2); \/\/ \u4f7f\u7528 :: \u8bbf\u95ee\u5176\u4e2d\u7684\u51fd\u6570\n    println!(&quot;{}&quot;, sum); \/\/ \u8f93\u51fa 3\n}<\/code><\/pre>\n<p>\u5728\u4f7f\u7528\u5b50\u6a21\u5757\u4e2d\u7684 struct \u3001 enum \u548c\u51fd\u6570\u7b49\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 :: \u6765\u67e5\u627e\uff0c\u5fc5\u8981\u65f6\u8fd8\u53ef\u4ee5\u501f\u52a9 super \u548c crate \u5173\u952e\u5b57\uff1a<\/p>\n<pre><code class=\"language-rust\">\/\/ utils \u5b50\u6a21\u5757\u4e2d\u7684 MyStruct\nutils::MyStruct\n\/\/ \u7236\u6a21\u5757\u7684 utils \u5b50\u6a21\u5757\u4e2d\u7684 MyStruct\nsuper::utils::MyStruct\n\/\/ crate \u6839\u6a21\u5757\u7684 utils \u5b50\u6a21\u5757\u4e2d\u7684 MyStruct\ncrate::utils::MyStruct<\/code><\/pre>\n<h2>\u516c\u5f00\u79c1\u6709\u8bbf\u95ee<\/h2>\n<p>\u5b50\u6a21\u5757\u5185\u90e8\u7684 struct \u3001 enum \u548c fn \u7b49\u5185\u5bb9\u4e0d\u80fd\u88ab\u5b50\u6a21\u5757\u5916\u90e8\u8bbf\u95ee\u5230\uff0c\u4f7f\u7528 pub \u53ef\u4ee5\u6539\u53d8\u8fd9\u4e9b\u5185\u5bb9\u7684\u53ef\u89c1\u6027\u3002\u53ef\u89c1\u6027\u53ef\u4ee5\u88ab\u8bbe\u5b9a\u4e3a\u4efb\u4f55\u4e00\u4e2a\u6a21\u5757\u5c42\u7ea7\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-rust\">\/\/ \u8fd9\u4e2a\u51fd\u6570\u53ea\u80fd\u5728\u5b50\u6a21\u5757\u5185\u4f7f\u7528\nfn my_private_function() { }\n\/\/ \u8fd9\u4e2a\u51fd\u6570\u5728\u7236\u6a21\u5757\u4e2d\u53ef\u8bbf\u95ee\npub(super) fn my_super_public_function() { }\n\/\/ \u8fd9\u4e2a\u51fd\u6570\u5728\u6574\u4e2a crate \u5185\u90e8\u53ef\u8bbf\u95ee\npub(crate) fn my_crate_public_function() { }\n\/\/ \u8fd9\u4e2a\u51fd\u6570\u5728\u6240\u6709\u5730\u65b9\u90fd\u53ef\u8bbf\u95ee\uff08\u5982\u679c\u6709\u522b\u7684 crate \u5f15\u7528\u8fd9\u4e2a crate \uff0c\u90a3\u5b83\u4e5f\u80fd\u8bbf\u95ee\uff09\npub fn my_public_function() { }<\/code><\/pre>\n<h2>use<\/h2>\n<p>\u5728\u5b50\u6a21\u5757\u5c42\u7ea7\u5f88\u591a\u65f6\uff0c\u6bcf\u6b21\u90fd\u4f7f\u7528 :: \u6765\u8bbf\u95ee\u7684\u8bdd\u4ee3\u7801\u4f1a\u5f88\u957f\uff0c\u901a\u5e38\u53ef\u4ee5\u7528 use \u6765\u7b80\u5316\u4ee3\u7801\u3002\u8fd9\u6837\u76f8\u5f53\u4e8e\u4e3a struct \u3001 enum \u548c fn \u7b49\u521b\u5efa\u4e86\u4e00\u4e2a\u8f83\u77ed\u7684\u522b\u540d:<\/p>\n<pre><code class=\"language-rust\">mod utils;\nuse utils::a_plus_b;\n\nfn main() {\n    let sum = a_plus_b(1, 2);\n    println!(&quot;{}&quot;, sum);\n}<\/code><\/pre>\n<p>use \u524d\u8fd8\u53ef\u4ee5\u7528 pub(&#8230;) \u6765\u4fee\u9970\uff0c\u4f7f\u8fd9\u4e2a\u522b\u540d\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u4e5f\u80fd\u8bbf\u95ee:<\/p>\n<pre><code class=\"language-rust\">\/\/ \u8fd9\u6837\u53ef\u4ee5\u4f7f\u5176\u4ed6\u6587\u4ef6\u4e2d\u90fd\u53ef\u4ee5\u8bbf\u95ee crate::a_plus_b\npub use utils::a_plus_b;<\/code><\/pre>\n<p>\u8fd8\u53ef\u4ee5\u7ed3\u5408 as \u5173\u952e\u5b57\u6765\u6539\u540d\uff1a<\/p>\n<pre><code class=\"language-rust\">\/\/ \u5c06\u5f15\u5165\u7684 a_plus_b \u6539\u540d\u4e3a plus\nuse utils::a_plus_b as plus;<\/code><\/pre>\n<p>\u8fd8\u53ef\u4ee5\u76f4\u63a5\u5c06\u4e00\u4e2a\u5b50\u6a21\u5757\u5185\u7684\u6240\u6709\u9879\u76ee\u5168\u90e8\u5f15\u5165\uff1a<\/p>\n<pre><code class=\"language-rust\">\/\/ \u5f15\u5165\u6240\u6709 utils \u4e2d\u7684\u9879\u76ee\nuse utils::*;<\/code><\/pre>\n<h2>struct \u5185\u90e8\u53ef\u89c1\u6027<\/h2>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5373\u4f7f struct \u662f\u5916\u90e8\u53ef\u8bbf\u95ee\u7684\uff0c\u5b83\u5185\u90e8\u7684\u5b57\u6bb5\u4ecd\u4e0d\u53ef\u88ab\u5916\u90e8\u8bbf\u95ee\u3002\u53ef\u4ee5\u4f7f\u7528 pub \u5173\u952e\u5b57\u5355\u72ec\u6307\u5b9a\u6bcf\u4e2a\u5b57\u6bb5\u7684\u53ef\u89c1\u6027:<\/p>\n<pre><code class=\"language-rust\">pub struct Rectangle {\n    pub(super) width: f32, \/\/ \u8fd9\u4e2a\u5b57\u6bb5\u5728\u7236\u6a21\u5757\u53ef\u8bbf\u95ee\n    height: f32, \/\/ \u8fd9\u4e2a\u5b57\u6bb5\u4ec5\u5b50\u6a21\u5757\u5185\u53ef\u8bbf\u95ee\n}<\/code><\/pre>\n<p>\u5982\u679c\u6709\u4efb\u4f55\u4e00\u4e2a\u5b57\u6bb5\u5728\u7236\u6a21\u5757\u4e2d\u4e0d\u53ef\u8bbf\u95ee\uff0c\u7236\u6a21\u5757\u5c31\u4e0d\u80fd\u76f4\u63a5\u521b\u5efa\u8fd9\u4e2a struct \u7684\u6570\u636e\u5b9e\u4f8b\uff0c\u5373\uff1a<\/p>\n<pre><code class=\"language-rust\">fn new() -&gt; Rectangle {\n    \/\/ \u53ea\u80fd\u5199\u5728\u5b50\u6a21\u5757\u5185\uff0c\u56e0\u4e3a height \u53ea\u6709\u5b50\u6a21\u5757\u5185\u53ef\u89c1\n    Rectangle {\n        width: 0.0,\n        height: 0.0,\n    }\n}<\/code><\/pre>\n<p>\u56e0\u6b64\uff0c\u63a7\u5236\u5185\u90e8\u5b57\u6bb5\u7684\u53ef\u89c1\u6027\u53ef\u4ee5\u9632\u6b62 struct \u88ab\u5916\u90e8\u4ee3\u7801\u4ee5\u9519\u8bef\u7684\u65b9\u5f0f\u521b\u5efa\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c struct \u5185\u90e8\u5b57\u6bb5\u90fd\u4e0d\u5e94\u8bbe\u4e3a pub \uff0c\u800c\u662f\u66b4\u9732 pub \u7684\u5173\u8054\u51fd\u6570\u4f9b\u5916\u90e8\u8bbf\u95ee\u3002<\/p>\n<p>\u53e6\u5916\uff0c enum \u7684\u6bcf\u4e2a\u5206\u652f\u7684\u53ef\u89c1\u6027\u8ddf\u968f enum \u672c\u8eab\uff0c\u4e0d\u80fd\u4e3a enum \u7684\u6bcf\u4e2a\u5206\u652f\u5355\u72ec\u6307\u5b9a\u53ef\u89c1\u6027\u3002<\/p>\n<h1>\u63a5\u53e3<\/h1>\n<p>\u4e0d\u5199\u4f8b\u5b50\u4e86\u3002<\/p>\n<h2>trait<\/h2>\n<p>\u4e0d\u540c\u4e8e\u4e00\u4e9b\u5e38\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c rust \u6ca1\u6709\u7ee7\u627f\u3001 interface \u7b49\u673a\u5236\u3002\u53d6\u800c\u4ee3\u4e4b\u7684\u662f trait \u3002<\/p>\n<p>trait \u53ef\u4ee5\u7528\u4e8e\u8868\u8fbe struct \u548c enum \u6240\u5177\u6709\u7684\u62bd\u8c61\u7279\u5f81\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u5546\u57ce\u5185\u6709\u591a\u4e2a\u5546\u54c1\uff0c\u5176\u4e2d\u6c34\u679c\u6709\u4fdd\u8d28\u671f\u7b49\u6570\u636e\uff0c\u4e66\u6709\u4f5c\u8005\u3001\u51fa\u7248\u793e\u7b49\u6570\u636e\uff0c\u4f46\u4ed6\u4eec\u90fd\u662f\u5546\u54c1\u3001\u6709\u5404\u81ea\u7684\u552e\u4ef7\u3002\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u7528\u4e00\u4e2a\u201c\u5546\u54c1\u201d trait \u5c06\u4ed6\u4eec\u5f52\u4e3a\u4e00\u7c7b\uff1a<\/p>\n<pre><code class=\"language-rust\">trait Product {\n    \/\/ trait \u4e2d\u53ef\u4ee5\u5305\u542b\u82e5\u5e72\u4e2a\u51fd\u6570\u58f0\u660e\uff08\u4ee5\u5206\u53f7\u7ed3\u5c3e\uff0c\u6ca1\u6709\u5177\u4f53\u5b9e\u73b0\uff09\n    fn price(&amp;self) -&gt; u32;\n}<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u4e3a\u6c34\u679c\u5b9e\u73b0 Product trait \uff1a<\/p>\n<pre><code class=\"language-rust\">struct Fruit {\n    durability_days: u32,\n    current_price: u32,\n}\n\n\/\/ \u5b9e\u73b0 Product trait \uff0c\u6b64\u65f6\u9700\u8981\u4e3a\u5176\u4e2d\u7684\u51fd\u6570\u6dfb\u52a0\u5b9e\u73b0\nimpl Product for Fruit {\n    fn price(&amp;self) -&gt; u32 {\n        self.current_price\n    }\n}<\/code><\/pre>\n<p>\u4e3a\u4e66\u4e5f\u5b9e\u73b0 Product trait \uff1a<\/p>\n<pre><code class=\"language-rust\">struct Book {\n    author: String,\n    publisher: String,\n    original_price: u32,\n    discount: f32,\n}\n\nimpl Product for Book {\n    fn price(&amp;self) -&gt; u32 {\n        let price = self.original_price as f32 * self.discount;\n        price.round() as u32\n    }\n}<\/code><\/pre>\n<p>\u5728\u5b9e\u73b0\u8d2d\u7269\u8f66\u7684\u65f6\u5019\uff0c\u5e76\u4e0d\u9700\u8981\u5173\u5fc3\u5177\u4f53\u662f\u6c34\u679c\u8fd8\u662f\u4e66\uff0c\u800c\u662f\u9488\u5bf9 Product trait \u8fdb\u884c\u5b9e\u73b0\uff0c\u5177\u4f53\u51fd\u6570\u4e2d\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a impl Product \u6765\u4ee3\u66ff\uff1a<\/p>\n<pre><code class=\"language-rust\">struct Cart {\n    total_price: u32,\n}\nimpl Cart {\n    fn new() -&gt; Self {\n        Self { total_price: 0 }\n    }\n    \/\/ \u51fd\u6570\u53c2\u6570\u53ef\u4ee5\u4e0d\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u7c7b\u578b\uff0c\u800c\u662f\u4e00\u4e2a trait\n    fn add_product(&amp;mut self, product: &amp; impl Product) {\n        self.total_price += product.price();\n    }\n    fn checkout(self) -&gt; u32 {\n        self.total_price\n    }\n}<\/code><\/pre>\n<p>\u4e3a impl Product \u4f20\u53c2\u65f6\uff0c\u53ef\u4ee5\u4f20\u4efb\u4f55\u5b9e\u73b0\u4e86 Product trait \u7684\u7c7b\u578b\uff1a<\/p>\n<pre><code class=\"language-rust\">fn main() {\n    let apple = Fruit {\n        durability_days: 30,\n        current_price: 5,\n    };\n    let taocp = Book {\n        author: format!(&quot;Knuth&quot;),\n        publisher: format!(&quot;AW&quot;),\n        original_price: 100,\n        discount: 0.5,\n    };\n    let mut cart = Cart::new();\n    cart.add_product(&amp;apple);\n    cart.add_product(&amp;taocp);\n    let total_price = cart.checkout();\n    println!(&quot;Total price: {}&quot;, total_price);\n}<\/code><\/pre>\n<p>\u50cf\u4e0a\u4f8b\u8fd9\u6837\uff0c trait \u7684\u4e3b\u8981\u4f5c\u7528\u5c31\u662f\u4f7f\u5f97 Cart \u7684\u5b9e\u73b0\u5e76\u4e0d\u9700\u8981\u5173\u5fc3\u5404\u79cd Product \u7684\u5177\u4f53\u5b9e\u73b0\uff0c\u89e3\u8026\u4ee3\u7801\u3002<\/p>\n<h2>\u5728 trait \u4e2d\u5b9e\u73b0\u51fd\u6570<\/h2>\n<p>\u5728 trait \u5b9a\u4e49\u91cc\uff0c\u9664\u4e86\u672a\u5b9e\u73b0\u7684\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u5305\u542b\u5e26\u6709\u5177\u4f53\u5b9e\u73b0\u7684\u51fd\u6570\u3002\u4f46\u5728\u51fd\u6570\u5b9e\u73b0\u4e2d\u53ea\u80fd\u8c03\u7528\u8fd9\u4e2a trait \u91cc\u5176\u4ed6\u51fd\u6570\uff0c\u4e0d\u80fd\u8bbf\u95ee\u5b9e\u73b0 trait \u7684 struct \uff08\u6216 enum \uff09\u91cc\u9762\u7684\u5177\u4f53\u5b57\u6bb5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u8ba1\u7b97\u591a\u4e2a\u76f8\u540c\u5546\u54c1\u603b\u4ef7\u7684\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-rust\">trait Product {\n    fn price(&amp;self) -&gt; u32;\n    fn price_of_multiple_items(&amp;self, count: u32) -&gt; u32 {\n        self.price() * count\n    }\n}<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u5728 trait \u4e2d\u5b9e\u73b0\u4e86\u7684\u51fd\u6570\uff0c\u4f9d\u65e7\u53ef\u4ee5\u88ab struct \u6216 enum \u5728\u5b9e\u73b0\u8fd9\u4e2a trait \u65f6\u8986\u76d6\uff08\u867d\u7136\u5b9e\u8df5\u4e2d\u5f88\u5c11\u8fd9\u4e48\u505a\uff09\u3002<\/p>\n<h2>\u5173\u8054\u7c7b\u578b<\/h2>\n<p>\u5982\u679c trait \u4e2d\u7684\u51fd\u6570\u4e2d\u5305\u542b\u4e00\u4e9b\u5728 trait \u4e2d\u8fd8\u4e0d\u786e\u5b9a\u7684\u7c7b\u578b\uff0c\u53ef\u4ee5\u5728 trait \u91cc\u7528 type \u5b9a\u4e49\u51fa\u6765\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6c34\u679c\u662f\u6309\u91cd\u91cf\u8ba1\u4ef7\uff0c\u4e66\u662f\u6309\u6570\u91cf\u8ba1\u4ef7\u7684\uff0c\u5c31\u8981\u6539\u6210\u4e0b\u9762\u8fd9\u6837\u7684\u4ef7\u683c\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-rust\">trait Product {\n    type AmountType; \/\/ \u5b9a\u4e49\u4e00\u4e2a\u5173\u8054\u7c7b\u578b\n    fn price_of_amount(&amp;self, amount: Self::AmountType) -&gt; u32;\n}<\/code><\/pre>\n<p>\u5176\u4e2d\u7684 amount \u53ef\u80fd\u662f\u91cd\u91cf\u6570\u503c f32 \u6216\u8005\u6570\u91cf u32 \uff0c\u5177\u4f53\u662f\u54ea\u4e2a\u7c7b\u578b\uff0c\u9700\u8981\u5728 impl Product \u4e2d\u660e\u786e\u6307\u5b9a\uff1a<\/p>\n<pre><code class=\"language-rust\">struct Fruit {\n    durability_days: u32,\n    unit_price: u32,\n}\nimpl Product for Fruit {\n    type AmountType = f32; \/\/ \u6307\u5b9a\u5173\u8054\u7c7b\u578b\u7684\u5b9e\u9645\u7c7b\u578b\n    fn price_of_amount(&amp;self, amount: Self::AmountType) -&gt; u32 {\n        let price = self.unit_price as f32 * amount;\n        price.round() as u32\n    }\n}\nstruct Book {\n    author: String,\n    publisher: String,\n    original_price: u32,\n    discount: f32,\n}\nimpl Product for Book {\n    type AmountType = u32; \/\/ \u6307\u5b9a\u5173\u8054\u7c7b\u578b\u7684\u5b9e\u9645\u7c7b\u578b\n    fn price_of_amount(&amp;self, amount: Self::AmountType) -&gt; u32 {\n        let price = self.original_price as f32 * self.discount;\n        price.round() as u32 * amount\n    }\n}<\/code><\/pre>\n<p>\u5728\u5b9e\u9645\u4f7f\u7528 price_of_amount \u65b9\u6cd5\u65f6\uff0c\u4f20\u5165\u503c\u7684\u7c7b\u578b\u9700\u8981\u4e0e\u5bf9\u5e94\u7684\u5173\u8054\u7c7b\u578b\u4e00\u81f4\uff1a<\/p>\n<pre><code class=\"language-rust\">fn main() {\n    let apple = Fruit {\n        durability_days: 30,\n        unit_price: 5,\n    };\n    let taocp = Book {\n        author: format!(&quot;Knuth&quot;),\n        publisher: format!(&quot;AW&quot;),\n        original_price: 100,\n        discount: 0.5,\n    };\n    let mut total_price = 0;\n    total_price += apple.price_of_amount(1.5);\n    total_price += taocp.price_of_amount(2);\n    println!(&quot;Total price: {}&quot;, total_price);\n}<\/code><\/pre>\n<h2>\u5185\u7f6e trait<\/h2>\n<p>rust \u8bed\u8a00\u672c\u8eab\u4e5f\u6709\u4e00\u4e9b\u5185\u7f6e\u7684 trait \uff0c\u5982 Debug \u3001 Display \u7b49\uff0c\u5b9e\u73b0\u8fd9\u4e9b\u5185\u7f6e trait \u53ef\u4ee5\u6539\u53d8\u4e00\u4e9b rust \u7f16\u8bd1\u5668\u7684\u884c\u4e3a\u3002<\/p>\n<p>Debug trait \u53ef\u4ee5\u6539\u53d8 struct \u6216 enum \u5728 println! \u4e2d\u88ab {:?} \u8f93\u51fa\u65f6\u7684\u5177\u4f53\u663e\u793a\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u666e\u901a\u7684 struct \u4f1a\u4ea7\u751f\u56fa\u5b9a\u683c\u5f0f\u7684\u8f93\u51fa\uff1a<\/p>\n<pre><code class=\"language-rust\">struct Rect {\n    width: f32,\n    height: f32,\n}\nfn main() {\n    let rect = Rect {\n        width: 2.,\n        height: 3.,\n    };\n    println!(&quot;{:?}&quot;, rect);\n    \/\/ \u8f93\u51fa\uff1aRect { width: 2.0, height: 3.0 }\n}<\/code><\/pre>\n<p>\u901a\u8fc7 impl Debug \u5c31\u53ef\u4ee5\u81ea\u5b9a\u4e49\u8fd9\u4e2a\u8f93\u51fa\u683c\u5f0f\uff1a<\/p>\n<pre><code class=\"language-rust\">use std::fmt::{Debug, Formatter};\nstruct Rect {\n    width: f32,\n    height: f32,\n}\nimpl Debug for Rect {\n    fn fmt(&amp;self, f: &amp;mut Formatter) -&gt; std::fmt::Result {\n        write!(f, &quot;Rect({}x{})&quot;, self.width, self.height)\n    }\n}\nfn main() {\n    let rect = Rect {\n        width: 2.,\n        height: 3.,\n    };\n    println!(&quot;{:?}&quot;, rect);\n    \/\/ \u8f93\u51fa\uff1aRect(2x3)\n}<\/code><\/pre>\n<p>\u5728 println! \u4e2d\u88ab {} \u8f93\u51fa\u65f6\u7684\u5177\u4f53\u663e\u793a\u5185\u5bb9\u53ef\u4ee5\u901a\u8fc7 impl Display \u6765\u6539\u53d8\uff0c\u5177\u4f53\u65b9\u6cd5\u7c7b\u4f3c\u3002<\/p>\n<p>rust \u63d0\u4f9b\u4e86\u5f88\u591a\u8fd9\u6837\u7684\u7c7b\u4f3c trait \uff0c\u53ef\u4ee5\u6539\u53d8\u8bed\u8a00\u672c\u8eab\u7684\u57fa\u7840\u529f\u80fd\uff0c\u5305\u62ec\u8ba9 struct \u6216\u8005 enum \u652f\u6301\u8fd0\u7b97\u7b26\uff08\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff09\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0\u4e00\u4e9b std::ops \u91cc\u9762\u7684\u4e00\u4e9b trait \u6765\u505a\u5230\u3002<\/p>\n<h1>\u8303\u578b<\/h1>\n<h2>struct \u548c enum \u7684\u7c7b\u578b\u53c2\u6570<\/h2>\n<p>\u6709\u65f6\uff0c\u5728 struct \u6216 enum \u5b9a\u4e49\u65f6\uff0c\u65e0\u6cd5\u786e\u5b9a\u5176\u4e2d\u67d0\u9879\u7684\u5177\u4f53\u7c7b\u578b\uff0c\u6b64\u65f6\u53ef\u4ee5\u5148\u7528\u201c\u7c7b\u578b\u53c2\u6570\u201d\u6765\u4ee3\u66ff\u5177\u4f53\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u957f\u65b9\u5f62\u5305\u542b\u957f\u3001\u5bbd\u4e24\u9879\uff0c\u6bcf\u4e00\u9879\u7684\u6570\u503c\u65e2\u53ef\u80fd\u662f\u6574\u6570\u7c7b\u578b u32 \u4e5f\u53ef\u80fd\u662f\u5c0f\u6570\u7c7b\u578b f32 \uff0c\u6b64\u65f6\u53ef\u4ee5\u5c06\u5b83\u4eec\u7684\u7c7b\u578b\u7528\u53c2\u6570 T \u548c U \u6765\u4ee3\u66ff\uff1a<\/p>\n<pre><code class=\"language-rust\">struct Rectangle&lt;T, U&gt; {\n    width: T,\n    height: U,\n}<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u4e0a\u9762\u8fd9\u79cd\u5b9a\u4e49\u65b9\u5f0f\u5c06\u5141\u8bb8\u957f\u548c\u5bbd\u662f\u4e0d\u540c\u7684\u7c7b\u578b\uff08\u53ef\u4ee5\u4e00\u4e2a\u662f u32 \u3001\u53e6\u4e00\u4e2a\u662f f32 \uff09\uff0c\u5982\u679c\u8981\u6c42\u957f\u548c\u5bbd\u7684\u7c7b\u578b\u76f8\u540c\uff0c\u5e94\u5f53\u53ea\u4f7f\u7528\u4e00\u4e2a\u53c2\u6570 T \uff1a<\/p>\n<pre><code class=\"language-rust\">struct Rectangle&lt;T&gt; {\n    width: T,\n    height: T,\n}<\/code><\/pre>\n<p>\u521b\u5efa\u5b83\u7684\u5b9e\u4f8b\u65f6\uff0c T \u9700\u8981\u6709\u4e00\u4e2a\u786e\u5b9a\u7684\u7c7b\u578b\uff0c\u53ef\u4ee5\u5199\u6210\uff1a<\/p>\n<pre><code class=\"language-rust\">let rect = Rectangle::&lt;u32&gt; {\n    width: 2,\n    height: 3,\n};<\/code><\/pre>\n<p>\u5176\u5b9e\u53ef\u4ee5\u4e0d\u5199\u660e\u5177\u4f53\u7c7b\u578b\uff0c\u7f16\u8bd1\u5668\u4f1a\u6839\u636e\u5177\u4f53\u503c\u7684\u7c7b\u578b\u6765\u63a8\u65ad\u51fa T \u7684\u5b9e\u9645\u7c7b\u578b\uff1a<\/p>\n<pre><code class=\"language-rust\">let rect = Rectangle {\n    width: 2,\n    height: 3,\n};<\/code><\/pre>\n<h2>\u6cdb\u578b\u7ea6\u675f<\/h2>\n<p>\u5b9e\u8df5\u4e2d\uff0c\u6cdb\u578b\u7684\u5b9e\u9645\u7c7b\u578b\u5f80\u5f80\u4e0d\u80fd\u662f\u4efb\u610f\u7c7b\u578b\u3002\u5728\u5b9a\u4e49\u65f6\uff0c\u5e38\u5e38\u4f1a\u8981\u6c42 T \u7684\u5b9e\u9645\u7c7b\u578b\u5fc5\u987b\u662f\u5b9e\u73b0\u4e86\u67d0\u4e9b trait \u7684\u7c7b\u578b\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-rust\">use std::fmt::Debug;\n\/\/ \u8981\u6c42 T \u7684\u5b9e\u9645\u7c7b\u578b\u5fc5\u987b\u5b9e\u73b0 Debug\nstruct Rectangle&lt;T: Debug&gt; {\n    width: T,\n    height: T,\n}\nimpl&lt;T: Debug&gt; Rectangle&lt;T&gt; {\n    fn debug_info(&amp;self) -&gt; String {\n        \/\/ \u56e0\u4e3a T \u5b9e\u73b0\u4e86 Debug \uff0c\u6240\u4ee5\u53ef\u4ee5\u7528 {:?} \u5360\u4f4d\n        format!(&quot;{:?}x{:?}&quot;, self.width, self.height)\n    }\n}\nfn main() {\n    let rect = Rectangle::&lt;u32&gt; {\n        width: 2,\n        height: 3,\n    };\n    println!(&quot;{}&quot;, rect.debug_info()); \/\/ \u8f93\u51fa 2x3\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5c06 T \u9650\u5236\u4e3a\u5b9e\u73b0\u4e86 Debug \u7684\u7c7b\u578b\uff0c\u4f46\u5728\u5b9e\u8df5\u4e2d\u8fd9\u79cd\u9650\u5236\u8fd8\u4e0d\u591f\u7cbe\u786e\u3002\u5982\u679c\u9700\u8981\u66f4\u7cbe\u786e\u7684\u9650\u5236\uff0c\u6bd4\u5982\u5c06 T \u9650\u5236\u4e3a u32 \u3001 f32 \u4e8c\u8005\u4e4b\u4e00\uff0c\u53ef\u4ee5\u5355\u72ec\u5b9a\u4e00\u4e2a\u65b0\u7684 trait \uff0c\u5e76\u5bf9 u32 \u548c f32 \u5b9e\u73b0\u8fd9\u4e2a\u65b0\u7684 trait \uff1a<\/p>\n<pre><code class=\"language-rust\">use std::fmt::Debug;\ntrait Length {\n    fn multiply(&amp;self, rhs: &amp;Self) -&gt; Self;\n}\n\/\/ \u4f7f u32 \u5b9e\u73b0 Length\nimpl Length for u32 {\n    fn multiply(&amp;self, rhs: &amp;u32) -&gt; u32 {\n        self * rhs\n    }\n}\n\/\/ \u4f7f f32 \u5b9e\u73b0 Length\nimpl Length for f32 {\n    fn multiply(&amp;self, rhs: &amp;f32) -&gt; f32 {\n        self * rhs\n    }\n}\n\/\/ T \u7684\u5b9e\u9645\u7c7b\u578b\u5fc5\u987b\u5b9e\u73b0 Debug \u548c Length\n\/\/ \u56e0\u4e3a\u53ea\u6709 u32 \u548c f32 \u5b9e\u73b0\u4e86 Length \uff0c\u6240\u4ee5\u5b9e\u9645\u53ea\u80fd\u662f\u5b83\u4eec\u4e4b\u4e00\nstruct Rectangle&lt;T: Debug + Length&gt; {\n    width: T,\n    height: T,\n}\nimpl&lt;T: Debug + Length&gt; Rectangle&lt;T&gt; {\n    fn area(&amp;self) -&gt; T {\n        \/\/ \u53ef\u4ee5\u8c03\u7528 Length \u4e2d\u7684 multiply \u65b9\u6cd5\u4e86\n        self.width.multiply(&amp;self.height)\n    }\n}\nfn main() {\n    let rect = Rectangle::&lt;f32&gt; {\n        width: 1.5,\n        height: 2.0,\n    };\n    println!(&quot;{}&quot;, rect.area()); \/\/ \u8f93\u51fa 3\n}<\/code><\/pre>\n<h2>\u5728\u51fd\u6570\u4e0a\u4f7f\u7528\u6cdb\u578b<\/h2>\n<p>\u9664\u4e86 struct \u548c enum \uff0c\u5728\u51fd\u6570\u548c trait \u4e0a\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u7c7b\u578b\u53c2\u6570\u3002\u5176\u4e2d\uff0c\u5728\u51fd\u6570\u4e0a\u7684\u5e94\u7528\u4e5f\u662f\u6bd4\u8f83\u5e38\u89c1\u7684\u3002\u5177\u4f53\u7528\u6cd5\u4e5f\u6bd4\u8f83\u7c7b\u4f3c\uff0c\u4f8b\u5982\uff0c\u4e0a\u9762\u7684 Length \u53ef\u4ee5\u7528\u5728\u51fd\u6570\u6cdb\u578b\u7ea6\u675f\u4e2d\uff1a<\/p>\n<pre><code class=\"language-rust\">fn rectangle_area&lt;T: Length&gt;(width: T, height: T) -&gt; T {\n    width.multiply(&amp;height)\n}<\/code><\/pre>\n<p>\u4e0d\u8fc7\uff0c\u4e3a\u4e86\u66f4\u52a0\u76f4\u89c2\uff0c\u901a\u5e38 T \u7684\u7ea6\u675f\u4f1a\u4f7f\u7528 where \u9644\u52a0\u5728\u540e\u9762\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-rust\">fn rectangle_area&lt;T&gt;(width: T, height: T) -&gt; T\nwhere T: Length {\n    width.multiply(&amp;height)\n}<\/code><\/pre>\n<p>\u53e6\u5916\uff0c\u5982\u679c T \u53ea\u5728\u53c2\u6570\u5217\u8868\u4e2d\u4f7f\u7528\u4e00\u6b21\uff0c\u90a3\u53ef\u4ee5\u4f7f\u7528 impl Trait \u7684\u5199\u6cd5\u7b80\u5316\uff0c\u5373\u4ee5\u4e0b\u4e24\u79cd\u5199\u6cd5\u7b49\u4ef7\uff1a<\/p>\n<pre><code class=\"language-rust\">fn print_square_area&lt;T&gt;(edge_len: T)\nwhere T: Debug + Length {\n    println!(&quot;{:?}&quot;, edge_len.multiply(&amp;edge_len));\n}\n\/\/ \u7b49\u4ef7\u4e8e\nfn print_square_area(edge_len: impl Debug + Length) {\n    println!(&quot;{:?}&quot;, edge_len.multiply(&amp;edge_len));\n}<\/code><\/pre>\n<p>\u5728\u5b9e\u8df5\u4e2d\u6cdb\u578b\u662f\u5f88\u5e38\u89c1\u7684\uff0c\u4f7f\u7528\u7a7a\u7c7b\u578b\u3001\u9519\u8bef\u5904\u7406\u65f6\u90fd\u4f1a\u6d89\u53ca\u6cdb\u578b\u3002<\/p>\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>\u6a21\u5757\u5316 \u51fd\u6570 \u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u80fd\u7701\u7565\uff1a fn a_plus_b(a: i32, b: i<a href=\"https:\/\/blog.indeex.club\/index.php\/2022\/02\/02\/web-rust%e5%9f%ba%e7%a1%80%e4%b8%89\/\" 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\/256"}],"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=256"}],"version-history":[{"count":2,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/256\/revisions"}],"predecessor-version":[{"id":274,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/256\/revisions\/274"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}