{"id":23263,"date":"2024-11-29T10:27:47","date_gmt":"2024-11-29T02:27:47","guid":{"rendered":"https:\/\/aif.amtbbs.org\/?p=23263"},"modified":"2024-11-29T10:28:38","modified_gmt":"2024-11-29T02:28:38","slug":"%e5%af%bc%e8%87%b4rust%e5%86%85%e5%ad%98%e6%b3%84%e6%bc%8f%e7%9a%84%e5%9b%9b%e7%a7%8d%e6%83%85%e5%86%b5%e5%8f%8a%e5%a6%82%e4%bd%95%e4%bf%ae%e5%a4%8d","status":"publish","type":"post","link":"https:\/\/aif.amtbbs.org\/index.php\/2024\/11\/29\/23263\/","title":{"rendered":"\u5bfc\u81f4Rust\u5185\u5b58\u6cc4\u6f0f\u7684\u56db\u79cd\u60c5\u51b5\u53ca\u5982\u4f55\u4fee\u590d"},"content":{"rendered":"<div class=\"article-desc\">\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86\u5728Rust\u7a0b\u5e8f\u4e2d\u5982\u4f55\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u4e0d\u540c\u76ee\u7684\u60c5\u51b5\u4e0b\u6a21\u62df\u5185\u5b58\u6cc4\u6f0f\uff0c\u4f8b\u5982\u5728\u8fd0\u884c\u65f6\u5728\u5185\u5b58\u4f4d\u7f6e\u4e2d\u4f7f\u7528\u6301\u4e45\u53d8\u91cf\u7b49\u3002\u4e86\u89e3Rust\u7684\u6240\u6709\u6743\u3001\u501f\u7528\u548cunsafe\u7684\u57fa\u672c\u539f\u7406\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7ba1\u7406\u5185\u5b58\u548c\u51cf\u5c11\u5185\u5b58\u6cc4\u6f0f\u3002<\/div>\n<div id=\"postspictures\" class=\"article-content\">\n<div id=\"container\" class=\"container am-engine\" data-v-1d7a5742=\"\" data-element=\"root\">\n<p>Rust\u7684\u5185\u7f6e\u6240\u6709\u6743\u6a21\u578b\u548c\u7f16\u8bd1\u65f6\u68c0\u67e5\u964d\u4f4e\u4e86\u5185\u5b58\u6cc4\u6f0f\u7684\u53ef\u80fd\u6027\u548c\u98ce\u9669\uff0c\u4f46\u5b83\u4eec\u4ecd\u7136\u5f88\u6709\u53ef\u80fd\u53d1\u751f\u3002<\/p>\n<p>\u5185\u5b58\u6cc4\u6f0f\u4e0d\u8fdd\u53cd\u6240\u6709\u6743\u89c4\u5219\uff0c\u56e0\u6b64\u501f\u7528\u68c0\u67e5\u5668\u5141\u8bb8\u5b83\u4eec\u5728\u7f16\u8bd1\u65f6\u53ef\u4ee5\u7f16\u8bd1\u901a\u8fc7\u3002\u5185\u5b58\u6cc4\u6f0f\u662f\u4f4e\u6548\u7684\uff0c\u901a\u5e38\u4e0d\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u7279\u522b\u662f\u5728\u6709\u8d44\u6e90\u9650\u5236\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<p>\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u5c06\u4e0d\u5b89\u5168\u884c\u4e3a\u5d4c\u5165\u5230unsafe\u5757\u4e2d\uff0c\u5b83\u4e5f\u4f1a\u7f16\u8bd1\u901a\u8fc7\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u65e0\u8bba\u64cd\u4f5c\u662f\u4ec0\u4e48\uff0c\u5185\u5b58\u5b89\u5168\u90fd\u662f\u4f60\u7684\u8d23\u4efb\uff0c\u4f8b\u5982\u6307\u9488\u89e3\u5f15\u7528\u3001\u624b\u52a8\u5185\u5b58\u5206\u914d\u6216\u5e76\u53d1\u95ee\u9898\u3002<\/p>\n<h3>\u6240\u6709\u6743\u548c\u501f\u7528\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u6f0f<\/h3>\n<p>\u501f\u7528\u68c0\u67e5\u5668\u5728\u7f16\u8bd1\u5668\u6267\u884c\u7a0b\u5e8f\u4e4b\u524d\u53ef\u4ee5\u9632\u6b62\u60ac\u7a7a\u5f15\u7528\u3001use-after-free\u9519\u8bef\u548c\u7f16\u8bd1\u65f6\u7684\u6570\u636e\u7ade\u4e89\u3002\u4f46\u662f\uff0c\u5728\u5206\u914d\u5185\u5b58\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u5728\u6574\u4e2a\u6267\u884c\u8fc7\u7a0b\u4e2d\u5220\u9664\u5185\u5b58\uff0c\u5219\u53ef\u80fd\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u5982\u4f55\u5b9e\u73b0\u53cc\u91cd\u94fe\u8868\u7684\u4e00\u4e2a\u4f8b\u5b50\u3002\u7a0b\u5e8f\u53ef\u4ee5\u6210\u529f\u8fd0\u884c\uff0c\u4f46\u4f1a\u51fa\u73b0\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\">\n<p><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_0\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\"><span class=\"token keyword\">use<\/span> std::rc::Rc<span class=\"token punctuation\">;<\/span>\r\n<span class=\"token keyword\">use<\/span> std::cell::RefCell<span class=\"token punctuation\">;<\/span>\r\n\r\nstruct Node {\r\n    <span class=\"token keyword\">value<\/span>: i32<span class=\"token punctuation\">,<\/span>\r\n    <span class=\"token keyword\">next<\/span>: <span class=\"token keyword\">Option<\/span><span class=\"token operator\">&lt;<\/span>Rc<span class=\"token operator\">&lt;<\/span>RefCell<span class=\"token operator\">&lt;<\/span>Node<span class=\"token operator\">&gt;&gt;<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">,<\/span>\r\n    <span class=\"token keyword\">prev<\/span>: <span class=\"token keyword\">Option<\/span><span class=\"token operator\">&lt;<\/span>Rc<span class=\"token operator\">&lt;<\/span>RefCell<span class=\"token operator\">&lt;<\/span>Node<span class=\"token operator\">&gt;&gt;<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">,<\/span>\r\n}\r\n\r\nfn main<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> {\r\n    let <span class=\"token keyword\">first<\/span> <span class=\"token operator\">=<\/span> Rc::new<span class=\"token punctuation\">(<\/span>RefCell::new<span class=\"token punctuation\">(<\/span>Node {\r\n        <span class=\"token keyword\">value<\/span>: <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span>\r\n        <span class=\"token keyword\">next<\/span>: None<span class=\"token punctuation\">,<\/span>\r\n        <span class=\"token keyword\">prev<\/span>: None<span class=\"token punctuation\">,<\/span>\r\n    }<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n    let <span class=\"token keyword\">second<\/span> <span class=\"token operator\">=<\/span> Rc::new<span class=\"token punctuation\">(<\/span>RefCell::new<span class=\"token punctuation\">(<\/span>Node {\r\n        <span class=\"token keyword\">value<\/span>: <span class=\"token number\">2<\/span><span class=\"token punctuation\">,<\/span>\r\n        <span class=\"token keyword\">next<\/span>: <span class=\"token keyword\">Some<\/span><span class=\"token punctuation\">(<\/span>Rc::clone<span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span><span class=\"token keyword\">first<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span>\r\n        <span class=\"token keyword\">prev<\/span>: <span class=\"token keyword\">Some<\/span><span class=\"token punctuation\">(<\/span>Rc::clone<span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span><span class=\"token keyword\">first<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span>\r\n    }<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n    <span class=\"token keyword\">first<\/span><span class=\"token punctuation\">.<\/span>borrow_mut<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token keyword\">next<\/span> <span class=\"token operator\">=<\/span> <span class=\"token keyword\">Some<\/span><span class=\"token punctuation\">(<\/span>Rc::clone<span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span><span class=\"token keyword\">second<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token keyword\">first<\/span><span class=\"token punctuation\">.<\/span>borrow_mut<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token keyword\">prev<\/span> <span class=\"token operator\">=<\/span> <span class=\"token keyword\">Some<\/span><span class=\"token punctuation\">(<\/span>Rc::clone<span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span><span class=\"token keyword\">second<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n    println<span class=\"token operator\">!<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Reference count of first: {}\"<\/span><span class=\"token punctuation\">,<\/span> Rc::strong_count<span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span><span class=\"token keyword\">first<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> \r\n    println<span class=\"token operator\">!<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Reference count of second: {}\"<\/span><span class=\"token punctuation\">,<\/span> Rc::strong_count<span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span><span class=\"token keyword\">second<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> \r\n\r\n}<\/code><\/pre>\n<ul id=\"code_id_0\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u95ee\u9898\u53d1\u751f\u5728\u4e24\u4e2a\u8282\u70b9\u4e4b\u95f4\u7684\u5faa\u73af\u5f15\u7528\u4e2d\uff0c\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002\u7531\u4e8eRC\u667a\u80fd\u6307\u9488\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e0d\u5904\u7406\u5faa\u73af\u5f15\u7528\uff0c\u56e0\u6b64\u6bcf\u4e2a\u8282\u70b9\u90fd\u6301\u6709\u5bf9\u53e6\u4e00\u4e2a\u8282\u70b9\u7684\u5f3a\u5f15\u7528\uff0c\u4ece\u800c\u5bfc\u81f4\u4e86\u5faa\u73af\u5f15\u7528\u3002<\/p>\n<p>\u5728main\u51fd\u6570\u6267\u884c\u4e4b\u540e\uff0csecond\u548cfirst\u53d8\u91cf\u7684\u5f15\u7528\u8ba1\u6570\u5c06\u7b49\u4e8efirst\u7684\u503c\uff0c\u5c3d\u7ba1\u5b83\u4e0d\u518d\u53ef\u8bbf\u95ee\u3002\u8fd9\u5c06\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff0c\u56e0\u4e3a\u6ca1\u6709\u4efb\u4f55\u8282\u70b9\u88ab\u91ca\u653e\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\">\n<p><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_1\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\">Reference count <span class=\"token keyword\">of<\/span> <span class=\"token keyword\">first<\/span>: <span class=\"token number\">3<\/span>\r\nReference count <span class=\"token keyword\">of<\/span> <span class=\"token keyword\">second<\/span>: <span class=\"token number\">3<\/span><\/code><\/pre>\n<ul id=\"code_id_1\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u4fee\u590d\u8fd9\u6837\u7684\u60c5\u51b5\uff1a<\/p>\n<ul data-id=\"u738a58b-56RE4SJR\">\n<li data-id=\"ld70c578-KWiD38f6\">\u5bf9\u4e00\u4e2a\u94fe\u8def\u65b9\u5411\u4f7f\u7528\u5f31\u5f15\u7528\uff0c\u5982weak&lt;T&gt;<\/li>\n<li data-id=\"ld70c578-EbcIXj6I\">\u5728\u51fd\u6570\u7ed3\u675f\u524d\u624b\u52a8\u6253\u7834\u5faa\u73af<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u662f\u5728prev\u5b57\u6bb5\u4e0a\u4f7f\u7528\u5f31\u6307\u9488\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4f8b\u5b50\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\">\n<p><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_2\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">use std::rc::{Rc, Weak};\r\nuse std::cell::RefCell;\r\n\r\nstruct Node {\r\n    value: i32,\r\n    next: Option&lt;Rc&lt;RefCell&lt;Node&gt;&gt;&gt;,\r\n    prev: Option&lt;Weak&lt;RefCell&lt;Node&gt;&gt;&gt;,\r\n}\r\n\r\nfn main() {\r\n    let first = Rc::new(RefCell::new(Node {\r\n        value: 1,\r\n        next: None,\r\n        prev: None,\r\n    }));\r\n\r\n    let second = Rc::new(RefCell::new(Node {\r\n        value: 2,\r\n        next: Some(Rc::clone(&amp;first)),\r\n        prev: Some(Rc::downgrade(&amp;first)),\r\n    }));\r\n\r\n    first.borrow_mut().next = Some(Rc::clone(&amp;second));\r\n    first.borrow_mut().prev = Some(Rc::downgrade(&amp;second));\r\n\r\n    println!(\"Reference count of first: {}\", Rc::strong_count(&amp;first)); \r\n    println!(\"Reference count of second: {}\", Rc::strong_count(&amp;second)); \r\n\r\n    println!(\"First value: {}\", first.borrow().value);\r\n    println!(\"Second value: {}\", second.borrow().value);\r\n\r\n    let next_of_first = first.borrow().next.as_ref().map(|r| r.borrow().value);\r\n    println!(\"Next of first: {}\", next_of_first.unwrap());\r\n\r\n    let prev_of_second = second.borrow().prev.as_ref().unwrap().upgrade().unwrap();\r\n    println!(\"Prev of second: {}\", prev_of_second.borrow().value);\r\n}<\/code><\/pre>\n<ul id=\"code_id_2\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<li>37.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u53ef\u4ee5\u4f7f\u7528Weak&lt;RefCell&lt;Node&gt;&gt;\u6765\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\uff0c\u56e0\u4e3a\u5f31\u5f15\u7528\u4e0d\u4f1a\u589e\u52a0\u5f3a\u5f15\u7528\u8ba1\u6570\uff0c\u5e76\u4e14\u8282\u70b9\u53ef\u4ee5\u88ab\u91ca\u653e\u3002<\/p>\n<p>\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\">\n<p><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_3\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\">Reference count <span class=\"token keyword\">of<\/span> <span class=\"token keyword\">first<\/span>: <span class=\"token number\">2<\/span>\r\nReference count <span class=\"token keyword\">of<\/span> <span class=\"token keyword\">second<\/span>: <span class=\"token number\">2<\/span>\r\n<span class=\"token keyword\">First<\/span> <span class=\"token keyword\">value<\/span>: <span class=\"token number\">1<\/span>\r\n<span class=\"token keyword\">Second<\/span> <span class=\"token keyword\">value<\/span>: <span class=\"token number\">2<\/span>\r\n<span class=\"token keyword\">Next<\/span> <span class=\"token keyword\">of<\/span> <span class=\"token keyword\">first<\/span>: <span class=\"token number\">2<\/span>\r\n<span class=\"token keyword\">Prev<\/span> <span class=\"token keyword\">of<\/span> <span class=\"token keyword\">second<\/span>: <span class=\"token number\">1<\/span><\/code><\/pre>\n<ul id=\"code_id_3\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3>std::mem::forget\u51fd\u6570<\/h3>\n<p>\u5728\u5fc5\u8981\u65f6\uff0c\u53ef\u4ee5\u6709\u610f\u5730\u4f7f\u7528std::mem::forget\u51fd\u6570\u6765\u6cc4\u6f0fRust\u9879\u76ee\u4e2d\u7684\u5185\u5b58\uff0c\u7f16\u8bd1\u5668\u8ba4\u4e3a\u5b83\u662f\u5b89\u5168\u7684\u3002<\/p>\n<p>\u5373\u4f7f\u6ca1\u6709\u56de\u6536\u5185\u5b58\uff0c\u4e5f\u4e0d\u4f1a\u6709\u4e0d\u5b89\u5168\u7684\u8bbf\u95ee\u6216\u5185\u5b58\u95ee\u9898\u3002<\/p>\n<p>std::mem::forget\u83b7\u53d6\u503c\u7684\u6240\u6709\u6743\uff0c\u5e76\u4e14\u5728\u4e0d\u8fd0\u884c\u6790\u6784\u51fd\u6570\u7684\u60c5\u51b5\u4e0bforget\u5b83\uff0c\u7531\u4e8e\u5185\u5b58\u4e2d\u4fdd\u5b58\u7684\u8d44\u6e90\u6ca1\u6709\u88ab\u91ca\u653e\uff0c\u56e0\u6b64\u5c06\u5b58\u5728\u5185\u5b58\u6cc4\u6f0f\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\">\n<p><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_4\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\"><span class=\"token keyword\">use<\/span> std::mem<span class=\"token punctuation\">;<\/span>\r\n\r\nfn main<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> {\r\n    let <span class=\"token keyword\">data<\/span> <span class=\"token operator\">=<\/span> Box::new<span class=\"token punctuation\">(<\/span><span class=\"token number\">42<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    mem::forget<span class=\"token punctuation\">(<\/span><span class=\"token keyword\">data<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n}<\/code><\/pre>\n<ul id=\"code_id_4\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5728\u8fd0\u884c\u65f6\uff0cRust\u8df3\u8fc7\u901a\u5e38\u7684\u6e05\u7406\u8fc7\u7a0b\uff0c\u6570\u636e\u53d8\u91cf\u7684\u503c\u4e0d\u4f1a\u88ab\u5220\u9664\uff0c\u5e76\u4e14\u4e3a\u6570\u636e\u5206\u914d\u7684\u5185\u5b58\u5728\u51fd\u6570\u6267\u884c\u540e\u6cc4\u6f0f\u3002<\/p>\n<h3>\u4f7f\u7528unsafe\u5757\u6cc4\u6f0f\u5185\u5b58<\/h3>\n<p>\u5728\u4f7f\u7528\u539f\u59cb\u6307\u9488\u65f6\uff0c\u9700\u8981\u81ea\u5df1\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\uff0c\u8fd9\u5c31\u6709\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002\u4ee5\u4e0b\u662f\u5728unsafe\u5757\u4e2d\u4f7f\u7528\u539f\u59cb\u6307\u9488\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u7684\u539f\u56e0\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\">\n<p><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_5\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\">fn main<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> {\r\n    let x <span class=\"token operator\">=<\/span> Box::new<span class=\"token punctuation\">(<\/span><span class=\"token number\">42<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    let raw <span class=\"token operator\">=<\/span> Box::into_raw<span class=\"token punctuation\">(<\/span>x<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> \r\n\r\n    unsafe {\r\n        println<span class=\"token operator\">!<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Memory is now leaked: {}\"<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">*<\/span>raw<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    }\r\n}<\/code><\/pre>\n<ul id=\"code_id_5\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5185\u5b58\u6ca1\u6709\u663e\u5f0f\u91ca\u653e\uff0c\u5e76\u4e14\u5728\u8fd0\u884c\u65f6\u5c06\u5b58\u5728\u5185\u5b58\u6cc4\u6f0f\u3002\u5728\u7a0b\u5e8f\u6267\u884c\u7ed3\u675f\u4e4b\u540e\uff0c\u5185\u5b58\u5c06\u88ab\u91ca\u653e\uff0c\u5185\u5b58\u4f7f\u7528\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<h3>\u6545\u610f\u7528Box::leak\u6cc4\u6f0f\u5185\u5b58<\/h3>\n<p>Box::leak\u51fd\u6570\u53ef\u4ee5\u6545\u610f\u6cc4\u6f0f\u5185\u5b58\uff0c\u5f53\u9700\u8981\u5728\u6574\u4e2a\u8fd0\u884c\u65f6\u4f7f\u7528\u4e00\u4e2a\u503c\u65f6\uff0c\u8fd9\u79cd\u65b9\u5f0f\u662f\u6b63\u786e\u7684\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\">\n<p><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_6\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\">fn main<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> {\r\n    let x <span class=\"token operator\">=<\/span> Box::new<span class=\"token punctuation\">(<\/span>String::<span class=\"token keyword\">from<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Hello, world!\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    let leaked_str: <span class=\"token operator\">&amp;<\/span>'static str <span class=\"token operator\">=<\/span> Box::leak<span class=\"token punctuation\">(<\/span>x<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    println<span class=\"token operator\">!<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Leaked string: {}\"<\/span><span class=\"token punctuation\">,<\/span> leaked_str<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n}<\/code><\/pre>\n<ul id=\"code_id_6\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u4e0d\u8981\u6ee5\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5982\u679c\u4f60\u9700\u8981\u9759\u6001\u5f15\u7528\u6765\u6ee1\u8db3\u7279\u5b9a\u7684API\u9700\u6c42\uff0c\u90a3\u4e48Box::leak\u662f\u6709\u7528\u7684\u3002<\/p>\n<h3>\u4fee\u590dRust\u4e2d\u7684\u5185\u5b58\u6cc4\u6f0f<\/h3>\n<p>\u4fee\u590d\u5185\u5b58\u6cc4\u6f0f\u7684\u9ec4\u91d1\u6cd5\u5219\u662f\u4ece\u4e00\u5f00\u59cb\u5c31\u907f\u514d\u5b83\u4eec\uff0c\u9664\u975e\u4f60\u7684\u7528\u4f8b\u9700\u8981\u8fd9\u6837\u505a\u3002\u9075\u5faa\u6240\u6709\u6743\u89c4\u5219\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\u3002\u4e8b\u5b9e\u4e0a\uff0c\u901a\u8fc7\u501f\u7528\u68c0\u67e5\u5668\uff0cRust\u5b9e\u65bd\u4e86\u5f88\u597d\u7684\u5185\u5b58\u7ba1\u7406\u5b9e\u8df5\uff1a<\/p>\n<p>1\uff0c\u5f53\u4f60\u9700\u8981\u5728\u4e0d\u8f6c\u79fb\u6240\u6709\u6743\u7684\u60c5\u51b5\u4e0b\u501f\u7528\u503c\u65f6\u4f7f\u7528\u5f15\u7528\u3002<\/p>\n<p>2\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528Miri\u5de5\u5177\u6765\u68c0\u6d4b\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\u5e76\u6355\u83b7\u4e0e\u5185\u5b58\u6cc4\u6f0f\u76f8\u5173\u7684\u9519\u8bef\u3002<\/p>\n<p>3\uff0c\u5728\u81ea\u5b9a\u4e49\u7c7b\u578b\u4e0a\u5b9e\u73b0Drop trait\u4ee5\u6e05\u7406\u5185\u5b58\u3002<\/p>\n<p>4\uff0c\u4e0d\u8981\u591a\u4f59\u5730\u4f7f\u7528std::mem::forget\u3002\u68c0\u67e5Box&lt;T&gt;\uff0c\u4ee5\u4fbf\u5728\u503c\u8d85\u51fa\u8303\u56f4\u65f6\u81ea\u52a8\u6e05\u7406\u5806\u5185\u5b58\u3002<\/p>\n<p>5\uff0c\u4e0d\u8981\u65e0\u7f18\u65e0\u6545\u5730\u5230\u5904throw unsafe\u5757\u3002<\/p>\n<p>6\uff0c\u4f7f\u7528Rc&lt;T&gt;\u6216Arc&lt;T&gt;\u5171\u4eab\u53d8\u91cf\u6240\u6709\u6743\u3002<\/p>\n<p>7\uff0c\u5bf9\u4e8e\u5185\u90e8\u53ef\u53d8\u6027\uff0c\u4f7f\u7528RefCell&lt;T&gt;\u6216Mutex&lt;T&gt;\u3002\u5982\u679c\u9700\u8981\u786e\u4fdd\u5b89\u5168\u7684\u5e76\u53d1\u8bbf\u95ee\uff0c\u5b83\u4eec\u5f88\u6709\u5e2e\u52a9\u3002<\/p>\n<p>\u9075\u5faa\u8fd9\u4e9b\u6280\u5de7\u5e94\u8be5\u53ef\u4ee5\u5904\u7406Rust\u7a0b\u5e8f\u4e2d\u7684\u6240\u6709\u5185\u5b58\u6cc4\u6f0f\uff0c\u4ee5\u6784\u5efa\u4f4e\u5185\u5b58\u9700\u6c42\u7684Rust\u7a0b\u5e8f\u3002<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86\u5728Rust\u7a0b\u5e8f\u4e2d\u5982\u4f55\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u4e0d\u540c\u76ee\u7684\u60c5\u51b5\u4e0b\u6a21\u62df\u5185\u5b58\u6cc4\u6f0f\uff0c\u4f8b\u5982\u5728\u8fd0\u884c\u65f6\u5728\u5185\u5b58\u4f4d\u7f6e\u4e2d\u4f7f\u7528\u6301\u4e45\u53d8\u91cf\u7b49\u3002\u4e86\u89e3Rust\u7684\u6240\u6709\u6743\u3001\u501f\u7528\u548cunsafe\u7684\u57fa\u672c\u539f\u7406\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7ba1\u7406\u5185\u5b58\u548c\u51cf\u5c11\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p>\u6587\u7ae0\u6765\u81ea\uff1a51CTO<\/p>\n<\/div>\n<\/div>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_23263\" class=\"pvc_stats total_only  \" data-element-id=\"23263\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86\u5728Rust\u7a0b\u5e8f\u4e2d\u5982\u4f55\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u4e0d\u540c\u76ee\u7684\u60c5\u51b5\u4e0b\u6a21\u62df\u5185\u5b58\u6cc4\u6f0f\uff0c\u4f8b\u5982\u5728\u8fd0\u884c\u65f6\u5728\u5185\u5b58\u4f4d\u7f6e\u4e2d [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_23263\" class=\"pvc_stats total_only  \" data-element-id=\"23263\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":56,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,20,80],"tags":[1456],"class_list":["post-23263","post","type-post","status-publish","format-standard","hentry","category-23","category-20","category-80","tag-rust"],"_links":{"self":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/23263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/users\/56"}],"replies":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/comments?post=23263"}],"version-history":[{"count":2,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/23263\/revisions"}],"predecessor-version":[{"id":23265,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/23263\/revisions\/23265"}],"wp:attachment":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/media?parent=23263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/categories?post=23263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/tags?post=23263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}