{"id":19118,"date":"2023-03-01T19:10:45","date_gmt":"2023-03-01T11:10:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=19118"},"modified":"2023-07-30T23:02:45","modified_gmt":"2023-07-30T15:02:45","slug":"rust%e8%a7%a3%e5%bc%95%e7%94%a8%e5%86%8d%e5%bc%95%e7%94%a8%e6%98%af%e4%bb%80%e4%b9%88%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/19118.html","title":{"rendered":"rust\u89e3\u5f15\u7528\u518d\u5f15\u7528\u662f\u4ec0\u4e48\u64cd\u4f5c"},"content":{"rendered":"<blockquote class=\"wp-block-quote\">\n<p>\u5728 Rust \u4e2d\uff0c\u89e3\u5f15\u7528\u518d\u5f15\u7528\uff08Deref Coercion\uff09\u662f\u4e00\u79cd\u9690\u5f0f\u8f6c\u6362\uff0c\u5b83\u53ef\u4ee5\u5c06\u5b9e\u73b0\u4e86 Deref trait \u7684\u7c7b\u578b\u7684\u5f15\u7528\u8f6c\u6362\u4e3a\u7ecf\u8fc7 Deref \u540e\u6240\u5f97\u5230\u7684\u7c7b\u578b\u7684\u5f15\u7528\u3002\u5982\u679c\u4e00\u4e2a\u7c7b\u578b T \u5b9e\u73b0\u4e86 Deref trait\uff0c\u90a3\u4e48\u5bf9\u4e8e\u4e00\u4e2a\u7c7b\u578b\u4e3a &amp;T \u7684\u503c\uff0c\u5982\u679c\u5728\u4f7f\u7528\u5b83\u7684\u65f6\u5019\u9700\u8981 T \u7c7b\u578b\u7684\u503c\uff0c\u5219 Rust \u4f1a\u81ea\u52a8\u8c03\u7528 Deref \u65b9\u6cd5\u3002<\/p>\n<\/blockquote>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img fetchpriority=\"high\" alt=\"\" class=\"wp-image-19119\" decoding=\"async\" height=\"421\" sizes=\"(max-width: 680px) 100vw, 680px\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/03\/\u6a21\u677f2-\u526f\u672c-\u526f\u672c-\u526f\u672c-\u526f\u672c-2023-03-01T191017.312.jpg?x-oss-process=image\/auto-orient,1\/format,webp\" srcset=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/03\/\u6a21\u677f2-\u526f\u672c-\u526f\u672c-\u526f\u672c-\u526f\u672c-2023-03-01T191017.312.jpg?x-oss-process=image\/auto-orient,1\/format,webp 680w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/03\/\u6a21\u677f2-\u526f\u672c-\u526f\u672c-\u526f\u672c-\u526f\u672c-2023-03-01T191017.312-300x186.jpg 300w\" width=\"680\"\/><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\">\u4e00\u3001rust\u89e3\u5f15\u7528\u518d\u5f15\u7528<\/h2>\n<p>\u5728 Rust \u4e2d\uff0c\u89e3\u5f15\u7528\u518d\u5f15\u7528\uff08Deref Coercion\uff09\u662f\u4e00\u79cd\u9690\u5f0f\u8f6c\u6362\uff0c\u5b83\u53ef\u4ee5\u5c06\u5b9e\u73b0\u4e86 Deref trait \u7684\u7c7b\u578b\u7684\u5f15\u7528\u8f6c\u6362\u4e3a\u7ecf\u8fc7 Deref \u540e\u6240\u5f97\u5230\u7684\u7c7b\u578b\u7684\u5f15\u7528\u3002<\/p>\n<p>\u5177\u4f53\u6765\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u7c7b\u578b T \u5b9e\u73b0\u4e86 Deref trait\uff0c\u90a3\u4e48\u5bf9\u4e8e\u4e00\u4e2a\u7c7b\u578b\u4e3a &amp;T \u7684\u503c\uff0c\u5982\u679c\u5728\u4f7f\u7528\u5b83\u7684\u65f6\u5019\u9700\u8981 T \u7c7b\u578b\u7684\u503c\uff0c\u5219 Rust \u4f1a\u81ea\u52a8\u8c03\u7528 Deref \u65b9\u6cd5\uff0c\u5c06 &amp;T \u8f6c\u6362\u4e3a &amp;U\uff0c\u5176\u4e2d U \u662f T \u6240\u6307\u5411\u7684\u7c7b\u578b\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u662f\u89e3\u5f15\u7528\u3002\u7136\u540e\uff0c\u5982\u679c\u9700\u8981 <code>U<\/code> \u7c7b\u578b\u7684\u503c\uff0c\u800c\u5f53\u524d\u6709\u4e00\u4e2a &amp;U \u7c7b\u578b\u7684\u503c\uff0c\u5219 Rust \u4f1a\u81ea\u52a8\u5c06\u5176\u8f6c\u6362\u4e3a &amp;T\uff0c\u5373\u8fdb\u884c\u5f15\u7528\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u662f\u5f15\u7528\u3002<\/p>\n<p>\u8fd9\u4e2a\u8fc7\u7a0b\u7684\u5177\u4f53\u5b9e\u73b0\u662f\u7531 Rust \u7f16\u8bd1\u5668\u5b8c\u6210\u7684\uff0c\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u578b MyType\uff0c\u5e76\u4e14\u5b9e\u73b0\u4e86 Deref trait\uff0c\u90a3\u4e48\u5728\u4f7f\u7528 MyType \u7c7b\u578b\u7684\u5f15\u7528\u65f6\uff0c\u53ef\u4ee5\u7701\u7565\u5bf9 <code>*<\/code> \u7684\u663e\u5f0f\u8c03\u7528\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"wp-block-preformatted\">rustCopy code<code>struct MyType(String);\n\nimpl Deref for MyType {\n    type Target = String;\n\n    fn deref(&amp;self) -&gt; &amp;Self::Target {\n        &amp;self.0\n    }\n}\n\nfn main() {\n    let my_value = MyType(String::from(\"Hello, world!\"));\n    println!(\"{}\", my_value.len()); \/\/ \u7701\u7565\u4e86 * \u7684\u663e\u5f0f\u8c03\u7528\n}\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 my_value \u7684\u65b9\u6cd5\u548c\u5c5e\u6027\uff0c\u800c\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u5bf9\u5176\u8fdb\u884c\u89e3\u5f15\u7528\u64cd\u4f5c\uff0c\u8fd9\u662f\u56e0\u4e3a Rust \u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u8c03\u7528 Deref \u65b9\u6cd5\uff0c\u5c06 &amp;MyType \u8f6c\u6362\u4e3a &amp;String\u3002\u8fd9\u6837\uff0c\u4ee3\u7801\u5c31\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Rust \u4e2d\uff0c\u89e3\u5f15\u7528\u518d\u5f15\u7528\uff08Deref Coercion\uff09\u662f\u4e00\u79cd\u9690\u5f0f\u8f6c\u6362\uff0c\u5b83\u53ef\u4ee5\u5c06\u5b9e\u73b0\u4e86 Deref tr [&hellip;]","protected":false},"author":1,"featured_media":19120,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[119],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/19118"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=19118"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/19118\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/19120"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=19118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=19118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=19118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}