{"id":337,"date":"2024-05-20T23:25:22","date_gmt":"2024-05-20T15:25:22","guid":{"rendered":"https:\/\/cococat.top\/?p=337"},"modified":"2024-05-20T23:25:22","modified_gmt":"2024-05-20T15:25:22","slug":"rust-note-3-trait-cheatsheet","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2024\/05\/20\/rust-note-3-trait-cheatsheet\/","title":{"rendered":"rust \u7b14\u8bb03\uff1aTrait Cheatsheet"},"content":{"rendered":"<h2>1. Fn, FnMut, FnOnce<\/h2>\n<h3>1.1. \u8ba8\u8bba\u7684\u8303\u7574\uff1a\u95ed\u5305 or \u901a\u5e38\u51fd\u6570<\/h3>\n<p>\u8ba8\u8bba\u8fd9\u4e09\u4e2a trait \u65f6\uff0c\u901a\u5e38\u662f\u5173\u4e8e\u95ed\u5305\u7684\u3002\u666e\u901a\u7684 rust \u51fd\u6570\u88ab\u89c6\u4e3a\u5b9e\u73b0\u4e86\u4ee5\u4e0a\u6240\u6709\u4e09\u4e2atrait\u3002\u53ef\u4ee5\u7528\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u8bc1\u660e\uff1a<\/p>\n<pre><code class=\"language-rust\">fn apply_fn&lt;T&gt;(value: T, f: impl Fn(T) -&gt; T) -&gt; T {\n    f(value)\n}\n\nfn apply_fnmut&lt;T&gt;(value: T, mut f: impl FnMut(T) -&gt; T) -&gt; T {\n    f(value)\n}\n\nfn apply_fnonce&lt;T&gt;(value: T, f: impl FnOnce(T) -&gt; T) -&gt; T {\n    f(value)\n}\n\nfn square(num: i32) -&gt; i32 {\n    num * num\n}\n\nfn main() {\n    let result = apply_fn(5, square);\n    let result = apply_fnmut(5, square);\n    let result = apply_fnonce(5, square);\n\n    println!(&quot;{}&quot;, result);\n}<\/code><\/pre>\n<h3>1.2. \u4e09\u4e2a <code>Fn<\/code> \u7cfb\u5217 trait \u7684\u533a\u522b<\/h3>\n<p>\u5bf9\u4e8e\u95ed\u5305\u6765\u8bf4\uff0c\u7f16\u8bd1\u671f\u4f1a\u81ea\u52a8\u4e3a\u4ed6\u4eec\u5b9e\u73b0\u5408\u9002\u7684 <code>Fn<\/code> \u7cfb\u5217 trait\u3002\u5206\u4e09\u79cd\uff1a<\/p>\n<ul>\n<li><code>FnOnce<\/code> \u7ea6\u675f\u6700\u5f3a\uff0c\u8868\u793a\u95ed\u5305\u53ea\u80fd\u88ab\u6267\u884c\u4e00\u6b21\uff0c\u901a\u5e38\u662f\u7531\u4e8e\u53d6\u5f97\u4e86\u6355\u83b7\u503c\u7684\u6240\u6709\u6743\uff0c\u6216\u8005\u95ed\u5305\u672c\u8eab\u4f1a\u88ab\u6d88\u8017<\/li>\n<li><code>FnMut<\/code> \u7ea6\u675f\u6b21\u5f3a\uff0c\u8868\u793a\u95ed\u5305\u53ef\u4ee5\u88ab\u591a\u6b21\u8c03\u7528\uff0c\u4e14\u6301\u6709\u6355\u83b7\u53d8\u91cf\u7684\u53ef\u53d8\u5f15\u7528\u3002<\/li>\n<li><code>Fn<\/code> \u7ea6\u675f\u6700\u5f31\uff0c\u8868\u793a\u95ed\u5305\u53ef\u4ee5\u88ab\u91cd\u590d\u8c03\u7528\uff0c\u4e14\u6301\u6709\u6355\u83b7\u53d8\u91cf\u7684\u4e0d\u53ef\u53d8\u5f15\u7528\u3002<\/li>\n<\/ul>\n<p>\u5b9e\u9645\u4e0a\uff0c<code>FnOnce<\/code> \u662f <code>FnMut<\/code> \u7684\u8d85\u96c6\uff08supertrait\uff09\uff0c\u800c <code>FnMut<\/code> \u53c8\u662f <code>Fn<\/code> \u7684\u8d85\u96c6\u3002\u5373\u53ef\u4ee5\u7528\u4f2a\u7801\u7406\u89e3\uff1a<\/p>\n<pre><code class=\"language-rust\">trait FnOnce {}\ntrait FnMut: FnOnce {}\ntrait Fn: FnMut {}<\/code><\/pre>\n<p>\u56e0\u6b64\uff0c\u4e00\u4e2a\u8f83\u5f31\u7684\u95ed\u5305\u53ef\u4ee5\u88ab\u4f20\u5165\u66f4\u5f3a\u7684\u7ea6\u675f\u4e2d\uff0c\u4f8b\u5982\u4e00\u4e2a\u53ea\u5b9e\u73b0\u4e86 <code>Fn<\/code> \u7684\u95ed\u5305\u53ef\u4ee5\u88ab\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u8981\u6c42\u662f <code>FnOnce<\/code> \u7684\u573a\u5408\u3002\u4ece\u903b\u8f91\u6765\u8bf4\uff0c\u8fd9\u6837\u4e5f\u662f\u5b8c\u5168\u6210\u7acb\u7684\u3002<\/p>\n<h3>1.3. \u5173\u4e8e <code>FnOnce<\/code> \u7684\u8865\u5145\uff1a\u8c03\u7528\u540e\u88ab move\uff0c\u4ee5\u53ca\u95ed\u5305\u5b9e\u73b0 Copy<\/h3>\n<p>Rust \u7f16\u8bd1\u5668\u662f\u600e\u4e48\u9650\u5236 <code>FnOnce<\/code> \u53ea\u80fd\u8c03\u7528\u4e00\u6b21\u7684\u5462\uff1f\u5728 rustbook \u4e2d\u8bd1\u7248\u5bf9\u95ed\u5305\u7684\u4ecb\u7ecd\u7ae0\u8282\u4e2d\uff0c\u6709\u4e00\u4e2a\u5c1d\u8bd5\u91cd\u590d\u8c03\u7528\u7684<a href=\"https:\/\/course.rs\/advance\/functional-programing\/closure.html?highlight=fnmut#%E4%B8%89%E7%A7%8D-fn-%E7%89%B9%E5%BE%81\">\u4f8b\u5b50<\/a>\uff1a<\/p>\n<pre><code class=\"language-rust\">fn fn_once&lt;F&gt;(func: F)\nwhere\n    F: FnOnce(usize) -&gt; bool,\n{\n    println!(&quot;{}&quot;, func(3));\n    println!(&quot;{}&quot;, func(4));\n}<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre><code>error[E0382]: use of moved value: `func`\n   --&gt; src\/main.rs:118:20\n    |\n113 | fn fn_once&lt;F&gt;(func: F)\n    |               ---- move occurs because `func` has type `F`, which does not implement the `Copy` trait\n...\n117 |     println!(&quot;{}&quot;, func(3));\n    |                    ------- `func` moved due to this call\n118 |     println!(&quot;{}&quot;, func(4));\n    |                    ^^^^ value used here after move\n    |\nnote: this value implements `FnOnce`, which causes it to be moved when called\n   --&gt; src\/main.rs:117:20\n    |\n117 |     println!(&quot;{}&quot;, func(3));\n    |                    ^^^^\nhelp: consider further restricting this bound\n    |\n115 |     F: FnOnce(usize) -&gt; bool + Copy,\n    |                              ++++++<\/code><\/pre>\n<p>\u6839\u636e\u62a5\u9519\u4fe1\u606f\u53ef\u77e5\uff0c<code>FnOnce<\/code> \u7684\u95ed\u5305\u5728\u8c03\u7528\u540e\u4f1a\u88ab move \u6389\uff0c\u56e0\u6b64\u6211\u4eec\u4e0d\u518d\u80fd\u8c03\u7528\u5b83\u4e86\u3002\u4f46\u662f\u5982\u679c\u52a0\u4e0a <code>Copy<\/code> \u7684 trait bound\uff0c\u5c31\u80fd\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\u8fd9\u4e2a <code>func<\/code> \u3002\u67e5\u8be2 GPT\uff0c\u5f97\u77e5\u5982\u679c\u4e00\u4e2a\u95ed\u5305\u6355\u83b7\u7684\u53d8\u91cf\u90fd\u662f <code>Copy<\/code> \u7684\uff0c\u90a3\u4e48\u5b83\u4e5f\u662f <code>Copy<\/code> \u3002\u8fd9\u4e24\u4e2a\u6027\u8d28\u90fd\u80fd\u4f53\u73b0\u51fa\u95ed\u5305\u672c\u8eab\u5c31\u662f\u4e00\u79cd rust \u7c7b\u578b\uff0c\u4e00\u4e2a\u7c7b\u578b\u7684\u53d8\u91cf\u88ab move \u6389\u4ee5\u540e\u81ea\u7136\u4e0d\u80fd\u518d\u8bbf\u95ee\uff0c\u800c\u5982\u679c\u4e00\u4e2a\u7c7b\u578b\u91cc\u62e5\u6709\u7684\u5f15\u7528\u90fd\u662f <code>Copy<\/code> \u7684\uff0c\u90a3\u4e48\u5b83\u81ea\u7136\u4e5f\u662f <code>Copy<\/code> \u7684\u3002<\/p>\n<h3>1.4. \u8865\u5145\u4e4b\u4e8c\uff1a\u95ed\u5305\u7684\u5e95\u5c42\u539f\u7406<\/h3>\n<p>\u8fd9\u90e8\u5206\u8d44\u6599\u6765\u6e90\u4e8e Efficient Rust p14\u3002\u95ed\u5305\u7684\u5b9e\u73b0\u539f\u7406\u662f\uff0c\u7f16\u8bd1\u671f\u521b\u5efa\u4e00\u4e2a\u9690\u85cf\u7c7b\uff0c\u5b58\u653e\u6240\u6709\u9700\u8981\u88ab\u95ed\u5305\u6355\u83b7\u7684\u5f15\u7528\uff0c\u7136\u540e\u5c06\u95ed\u5305\u7684\u533f\u540d\u51fd\u6570\u4f53\u5b9e\u73b0\u4e3a\u6b64\u7c7b\u7684\u65b9\u6cd5\u3002<\/p>\n<h2>2. Send Sync<\/h2>\n<h2>3. Sized ?Sized<\/h2>\n<h2>4. Copy Clone<\/h2>\n<h2>5. Sink Stream<\/h2>\n<h2>6. Ref RefCell<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>1. Fn, FnMut, FnOnce 1.1. \u8ba8\u8bba\u7684\u8303\u7574\uff1a\u95ed\u5305 or \u901a\u5e38\u51fd\u6570 \u8ba8\u8bba\u8fd9\u4e09\u4e2a trait  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-337","post","type-post","status-publish","format-standard","hentry","category-rust"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=337"}],"version-history":[{"count":0,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/337\/revisions"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}