{"id":1602,"date":"2019-10-07T19:59:41","date_gmt":"2019-10-07T11:59:41","guid":{"rendered":"http:\/\/ykyi.net\/?p=1602"},"modified":"2019-10-07T19:59:41","modified_gmt":"2019-10-07T11:59:41","slug":"rust%e4%b8%ad%e7%9a%84str%e5%92%8cstring%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/ykyi.net\/?p=1602","title":{"rendered":"Rust\u4e2d\u7684&#038;str\u548cString\u6709\u4ec0\u4e48\u533a\u522b"},"content":{"rendered":"<p>&amp;str\u548cString\u8ba9Rust\u521d\u5b66\u8005\u56f0\u60d1\u4e0d\u5df2\u3002\u8fd9\u91cc\u7b80\u5355\u7684\u4ecb\u7ecd\u4e00\u4e2a&amp;str\u548cString\u7684\u533a\u522b\u3002<\/p>\n<p>String\u5c31\u50cf\u662fVec\u4e00\u6837\u662f\u52a8\u6001\u5206\u914d\u5728\u5806heap\u4e0a\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u5b83\u6307\u5411\u4e00\u4e32UTF8\u7f16\u7801\u7684\u5b57\u8282\u5e8f\u3002\u5f53\u9700\u8981\u62e5\u6709\u8be5\u5b57\u7b26\u4e32\uff0c\u8981\u66f4\u6539\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\uff0c\u8981\u628a\u5b57\u7b26\u4e32\u5728\u7ebf\u7a0b\u4e4b\u95f4\u4f20\u9012\uff0c\u8981\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u521b\u5efa\u5b57\u7b26\u4e32\u65f6\u4f7f\u7528\u6b64\u7c7b\u578b\u3002<\/p>\n<p>str\u4e5f\u6307\u5411\u4e00\u4e32UTF8\u7f16\u7801\u7684\u5b57\u8282\u5e8f\uff0c\u4f46\u5e76\u4e0d\u62e5\u6709\u6307\u5411\u7684\u5b57\u7b26\u4e32\uff0c\u800c\u4ec5\u4ec5\u53ea\u662f\u4e00\u4e2a\u201cstring slice\u201d\u3002<a href=\"https:\/\/doc.rust-lang.org\/book\/ch04-03-slices.html\">slice\u7684\u8be6\u7ec6\u89e3\u91ca\u70b9\u94fe\u63a5<\/a>\u3002str\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4ee5\u5f15\u7528<strong>&amp;str<\/strong>\u7684\u5f62\u5f0f\u51fa\u73b0\u3002\u53c8\u88ab\u79f0\u4e3a\u5b57\u7b26\u4e32\u5b57\u9762\u503c(string literal)\u3002<br \/>\n\u548cString\u6307\u5411\u7684\u5b57\u7b26\u4e32\u80af\u5b9a\u5206\u914d\u5728\u5806\u4e0a\u9762\u4e0d\u4e00\u6837\uff0cstr\u6307\u5411\u7684\u5b57\u7b26\u4e32\u53ef\u80fd\u51fa\u73b0\u5728\u5404\u79cd\u5730\u65b9\uff1a<br \/>\n1. <strong>\u5728\u7a0b\u5e8f\u7684\u5168\u5c40\u9759\u6001\u7a7a\u95f4<\/strong>(.text\u6bb5\uff1f\uff1f)\u3002\u4e00\u4e2astring literal\u7684\u7c7b\u578b\u548c\u751f\u547d\u671f\u662f\uff1a&amp;&#8217;static str\u3002\u5b57\u7b26\u4e32\u7684\u6570\u636e\u786c\u7f16\u7801\u8fdb\u751f\u6210\u7684\u7a0b\u5e8f\u6216\u5e93\u6587\u4ef6\uff0c\u4f46\u7a0b\u5e8f\u52a0\u8fd0\u884c\u65f6\uff0c\u5b57\u7b26\u4e32\u52a0\u8f7d\u8fdb\u5185\u5b58\u3002<br \/>\n2. <strong>\u5206\u914d\u5728\u5806\u4e0a\u9762<\/strong> String\u6709\u4e00\u4e2a\u65b9\u6cd5\u662f<a href=\"https:\/\/doc.rust-lang.org\/std\/string\/struct.String.html#method.as_str\">as_str<\/a>,\u5b83\u8fd4\u56de\u4e00\u4e2a&amp;str,\u6307\u5411String\u7ba1\u7406\u7684\u5806\u4e0a\u9762\u7684\u5b57\u7b26\u4e32\u3002\u53e6\u5916\uff0cString\u6709\u5b9e\u73b0Deref&lt;Target=str>\u8fd9\u6837\u4e00\u4e2aTrait\uff0c\u8ba9\u6240\u6709\u63a5\u53d7&amp;str\u4f5c\u4e3a\u53c2\u6570\u7684\u51fd\u6570\uff0c\u90fd\u53ef\u4ee5\u4f20String\u7684\u5f15\u7528\u3002\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u628a&amp;String\u8f6c\u6210&amp;str\u3002<br \/>\n3. <strong>\u5206\u914d\u5728\u6808\u4e0a\u9762<\/strong> \u5982\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u53d8\u91cfstack_str\u7684\u7c7b\u578b\u662f&amp;str,\u5b83\u6307\u5411\u6808\u4e0a\u9762\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code class=\"language-rust-lang \">use std::str;\n\nlet x: &amp;[u8] = &amp;[b'a', b'b', b'c'];\nlet stack_str: &amp;str = str::from_utf8(x).unwrap();\n<\/code><\/pre>\n<p>\u5982\u679c\u4ee5C++\u7a0b\u5e8f\u5458\u7684\u89c6\u89d2\u770bString\u548c&amp;str\u3002\u4e00\u4e2aRust\u7684String\u7c7b\u4f3cstd::string,\u5b8c\u5168\u62e5\u6709\u5e76\u7ba1\u7406\uff08\u521b\u5efa\uff0c\u9500\u6bc1\uff09\u9700\u8981\u7684\u5185\u5b58\u3002\u800cRust\u7684&amp;str\u5219\u7c7b\u4f3cchar*\uff08\u5f53\u7136\u5b9e\u9645\u4e0a\u8981\u590d\u6742\u5f88\u591a\uff09\uff0c\u4ec5\u4ec5\u6307\u5411\u5b57\u7b26\u4e32\uff0c\u4e0d\u8d1f\u8d23\u7ba1\u7406\u5bf9\u5e94\u7684\u5185\u5b58\u3002<\/p>\n<p>\u518d\u770b\u770bString\u548c&amp;str\u7684\u4e92\u8f6c\u5316\uff1a<br \/>\n&amp;str\u8f6cString\u53ef\u4ee5\u7528\u4e09\u79cd\u65b9\u6cd5<\/p>\n<pre><code class=\"language-rust-lang \">    let a: String = \"hello rust\".into();\n    let c: String = \"hello rust\".to_string();\n    let b: String = String::from(\"hello rust\");\n    println!(\"{}\", a);   \/\/ \u90fd\u8f93\u51fa hello rust\n    println!(\"{}\", b);\n    println!(\"{}\", c);\n<\/code><\/pre>\n<p>String\u8f6c&amp;str\u53ef\u4ee5\u76f4\u63a5\u5f15\u7528String\u7c7b\u578b\u7684\u503c\u6216\u8005\u8c03\u7528String\u7684as_str\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-rust-lang \">let a: String = String::from(\"hello rust\");\nlet aaa: &amp;str = &amp;a;\nlet bbb: &amp;str = a.as_str();\n<\/code><\/pre>\n<p><strong>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u94fe\u63a5<\/strong> <a href=\"http:\/\/ykyi.net\/2019\/10\/rust%E4%B8%AD%E7%9A%84str%E5%92%8Cstring%E6%9C%89%E4%BB%80%E4%B9%88%E5%8C%BA%E5%88%AB\/\">http:\/\/ykyi.net\/2019\/10\/rust%E4%B8%AD%E7%9A%84str%E5%92%8Cstring%E6%9C%89%E4%BB%80%E4%B9%88%E5%8C%BA%E5%88%AB\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&amp;str\u548cString\u8ba9Rust\u521d\u5b66\u8005\u56f0\u60d1\u4e0d\u5df2\u3002\u8fd9\u91cc\u7b80\u5355\u7684\u4ecb\u7ecd\u4e00\u4e2a&amp;str\u548cString\u7684\u533a\u522b\u3002 String\u5c31\u50cf\u662fVec\u4e00\u6837\u662f\u52a8\u6001\u5206\u914d\u5728\u5806heap\u4e0a\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u5b83\u6307\u5411\u4e00\u4e32UTF8\u7f16\u7801\u7684\u5b57\u8282\u5e8f\u3002\u5f53\u9700\u8981\u62e5\u6709\u8be5\u5b57\u7b26\u4e32\uff0c\u8981\u66f4\u6539\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\uff0c\u8981\u628a\u5b57\u7b26\u4e32\u5728\u7ebf\u7a0b\u4e4b\u95f4\u4f20\u9012\uff0c\u8981\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u521b\u5efa\u5b57\u7b26\u4e32\u65f6\u4f7f\u7528\u6b64\u7c7b\u578b\u3002 str\u4e5f\u6307\u5411\u4e00\u4e32UTF8\u7f16\u7801\u7684\u5b57\u8282\u5e8f\uff0c\u4f46\u5e76\u4e0d\u62e5\u6709\u6307\u5411\u7684\u5b57\u7b26\u4e32\uff0c\u800c\u4ec5\u4ec5\u53ea\u662f\u4e00\u4e2a\u201cstring slice\u201d\u3002slice\u7684\u8be6\u7ec6\u89e3\u91ca\u70b9\u94fe\u63a5\u3002str\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4ee5\u5f15\u7528&amp;str\u7684\u5f62\u5f0f\u51fa\u73b0\u3002\u53c8\u88ab\u79f0\u4e3a\u5b57\u7b26\u4e32\u5b57\u9762\u503c(string literal)\u3002 \u548cString\u6307\u5411\u7684\u5b57\u7b26\u4e32\u80af\u5b9a\u5206\u914d\u5728\u5806\u4e0a\u9762\u4e0d\u4e00\u6837\uff0cstr\u6307\u5411\u7684\u5b57\u7b26\u4e32\u53ef\u80fd\u51fa\u73b0\u5728\u5404\u79cd\u5730\u65b9\uff1a 1. \u5728\u7a0b\u5e8f\u7684\u5168\u5c40\u9759\u6001\u7a7a\u95f4(.text\u6bb5\uff1f\uff1f)\u3002\u4e00\u4e2astring literal\u7684\u7c7b\u578b\u548c\u751f\u547d\u671f\u662f\uff1a&amp;&#8217;static str\u3002\u5b57\u7b26\u4e32\u7684\u6570\u636e\u786c\u7f16\u7801\u8fdb\u751f\u6210\u7684\u7a0b\u5e8f\u6216\u5e93\u6587\u4ef6\uff0c\u4f46\u7a0b\u5e8f\u52a0\u8fd0\u884c\u65f6\uff0c\u5b57\u7b26\u4e32\u52a0\u8f7d\u8fdb\u5185\u5b58\u3002 2. \u5206\u914d\u5728\u5806\u4e0a\u9762 String\u6709\u4e00\u4e2a\u65b9\u6cd5\u662fas_str,\u5b83\u8fd4\u56de\u4e00\u4e2a&amp;str,\u6307\u5411String\u7ba1\u7406\u7684\u5806\u4e0a\u9762\u7684\u5b57\u7b26\u4e32\u3002\u53e6\u5916\uff0cString\u6709\u5b9e\u73b0Deref&lt;Target=str>\u8fd9\u6837\u4e00\u4e2aTrait\uff0c\u8ba9\u6240\u6709\u63a5\u53d7&amp;str\u4f5c\u4e3a\u53c2\u6570\u7684\u51fd\u6570\uff0c\u90fd\u53ef\u4ee5\u4f20String\u7684\u5f15\u7528\u3002\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u628a&amp;String\u8f6c\u6210&amp;str\u3002 3. \u5206\u914d\u5728\u6808\u4e0a\u9762 \u5982\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u53d8\u91cfstack_str\u7684\u7c7b\u578b\u662f&amp;str,\u5b83\u6307\u5411\u6808\u4e0a\u9762\u7684\u5b57\u7b26\u4e32\u3002 use std::str; let x: &amp;[u8] = &amp;[b&#8217;a&#8217;, b&#8217;b&#8217;, b&#8217;c&#8217;]; let stack_str: &amp;str = str::from_utf8(x).unwrap(); \u5982\u679c\u4ee5C++\u7a0b\u5e8f\u5458\u7684\u89c6\u89d2\u770bString\u548c&amp;str\u3002\u4e00\u4e2aRust\u7684String\u7c7b\u4f3cstd::string,\u5b8c\u5168\u62e5\u6709\u5e76\u7ba1\u7406\uff08\u521b\u5efa\uff0c\u9500\u6bc1\uff09\u9700\u8981\u7684\u5185\u5b58\u3002\u800cRust\u7684&amp;str\u5219\u7c7b\u4f3cchar*\uff08\u5f53\u7136\u5b9e\u9645\u4e0a\u8981\u590d\u6742\u5f88\u591a\uff09\uff0c\u4ec5\u4ec5\u6307\u5411\u5b57\u7b26\u4e32\uff0c\u4e0d\u8d1f\u8d23\u7ba1\u7406\u5bf9\u5e94\u7684\u5185\u5b58\u3002 \u518d\u770b\u770bString\u548c&amp;str\u7684\u4e92\u8f6c\u5316\uff1a &amp;str\u8f6cString\u53ef\u4ee5\u7528\u4e09\u79cd\u65b9\u6cd5 let a: String = &#8220;hello rust&#8221;.into(); let c: String = &#8220;hello rust&#8221;.to_string(); let b: String = String::from(&#8220;hello rust&#8221;); println!(&#8220;{}&#8221;, a); \/\/ \u90fd\u8f93\u51fa &hellip; <a href=\"https:\/\/ykyi.net\/?p=1602\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Rust\u4e2d\u7684&#038;str\u548cString\u6709\u4ec0\u4e48\u533a\u522b&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[72],"class_list":["post-1602","post","type-post","status-publish","format-standard","hentry","category-tech_articles","tag-rust"],"_links":{"self":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/1602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1602"}],"version-history":[{"count":0,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/1602\/revisions"}],"wp:attachment":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}