{"id":5282,"date":"2025-09-23T10:36:56","date_gmt":"2025-09-23T02:36:56","guid":{"rendered":"https:\/\/www.yizishun.com\/?p=5282"},"modified":"2025-09-23T10:36:56","modified_gmt":"2025-09-23T02:36:56","slug":"rcore-%e7%ac%ac%e9%9b%b6%e7%ab%a0%e7%bb%83%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/www.yizishun.com\/?p=5282","title":{"rendered":"rcore \u7b2c\u96f6\u7ae0\u7ec3\u4e60"},"content":{"rendered":"<h2>Ch0 Anwser<\/h2>\n<h2>1.\u914d\u73af\u5883<\/h2>\n<p>\u6211\u7528\u7684\u662fnixos\/macos\u53cc\u7cfb\u7edf\uff0c\u6211\u7684macos\u4e5f\u4f7f\u7528\u7684nix-darwin,\u6240\u4ee5\u6211\u57fa\u672c\u4e0a\u53ea\u7528\u5728nisos\u4e2d\u914d\u597dhome-manager,\u5c31\u53ef\u4ee5\u4e86\uff0c\u5f53\u7136\u5176\u5b9e\u6700\u597d\u7684\u65b9\u5f0f\u662f\u5199\u4e00\u4e2anix shell,\u7136\u540e\u6bcf\u6b21\u5de5\u4f5c\u7684\u65f6\u5019\u4f7f\u7528nix shell<\/p>\n<p>\u7136\u540eqemu\u6ca1\u4ec0\u4e48\u597d\u8bf4\u7684\uff0c\u52a0\u4e00\u4e2aqemu\u5305\u5373\u53ef\uff0c\u4f46\u662f\u4ea4\u53c9\u7f16\u8bd1\u5de5\u5177\u94fe\u53ef\u80fd\u548c\u666e\u901a\u7684\u5305\u4e0d\u4e00\u6837\uff0c\u9700\u8981\u5199<code>pkgsCross.riscv64-embedded.buildPackages.gcc<\/code> ,\u7136\u540e\u4f7f\u7528alias\u628ariscv64-none-elf-gdb \u5230riscv64-unknown-elf-gdb\uff0c\u7136\u540eclone\u4e0brcore\u7684\u4ed3\u5e93\uff0cmake run\u5373\u53ef\uff0c\u81f3\u4e8e\u4e3a\u5565\u662fnone,\u5177\u4f53\u662f\u7531\u4e8estdenv\u7684\u539f\u56e0<\/p>\n<h2>2.exception<\/h2>\n<blockquote><p>\u5728Linux\u73af\u5883\u4e0b\u7f16\u5199\u4e00\u4e2a\u4f1a\u4ea7\u751f\u5f02\u5e38\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u7b80\u8981\u89e3\u91ca\u64cd\u4f5c\u7cfb\u7edf\u7684\u5904\u7406\u7ed3\u679c<\/p><\/blockquote>\n<p>\u4f7f\u7528rust\u7f16\u5199\u4e00\u4e2a\u96640\u51fd\u6570\uff0c\u5c06\u62a5\u544a<\/p>\n<pre><code class=\"language-sh\">&gt; rustc exception.rs\nerror: this operation will panic at runtime\n --&gt; exception.rs:3:13\n  |\n3 |     let a = 1\/0;\n  |             ^^^ attempt to divide `1_i32` by zero\n  |\n  = note: `#[deny(unconditional_panic)]` on by default\n\nerror: aborting due to 1 previous error<\/code><\/pre>\n<p>\u5982\u679c\u4f7f\u7528\u4ed6\u7ed9\u7684\u5efa\u8bae<code>#![allow(unconditional_panic)]<\/code><\/p>\n<pre><code class=\"language-sh\">&gt; RUST_BACKTRACE=1 .\/exception \n\nthread 'main' panicked at exception.rs:3:13:\nattempt to divide by zero\nstack backtrace:\n   0: __rustc::rust_begin_unwind\n             at \/rustc\/29483883eed69d5fb4db01964cdf2af4d86e9cb2\/library\/std\/src\/panicking.rs:697:5\n   1: core::panicking::panic_fmt\n             at \/rustc\/29483883eed69d5fb4db01964cdf2af4d86e9cb2\/library\/core\/src\/panicking.rs:75:14\n   2: core::panicking::panic_const::panic_const_div_by_zero\n             at \/rustc\/29483883eed69d5fb4db01964cdf2af4d86e9cb2\/library\/core\/src\/panicking.rs:175:17\n   3: exception::main\n   4: core::ops::function::FnOnce::call_once\nnote: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.<\/code><\/pre>\n<p>\u4f1apanic\uff0c\u4f46\u662f\u770b\u8d77\u6765\u5e76\u4e0d\u662f\u64cd\u4f5c\u7cfb\u7edf\u7684panic,\u800c\u662funwind,\u7136\u540e\u8d70\u7684\u7f16\u7a0b\u8bed\u8a00\u7ea7\u522b\u7684panic\uff0c\u4e8e\u662f\u6253\u7b97\u4f7f\u7528c\u8bed\u8a00\u7f16\u5199<\/p>\n<pre><code class=\"language-sh\">&gt; gcc exception.c      \nexception.c: In function \u2018main\u2019:\nexception.c:3:15: warning: division by zero [-Wdiv-by-zero]\n    3 |     int a = 1 \/ 0;\n&gt; .\/a.out    \n0<\/code><\/pre>\n<p>\u5f88\u5947\u602a\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u4f1a\u51fa\u9519\uff0c\u662f\u4e0d\u662farm\u5bf9\u4e8e\u96640\u9519\u8bef\u5e76\u4e0d\u4f1a\u62a5\u5f02\u5e38<\/p>\n<p>\u641c\u4e86\u4e00\u4e0b\uff0c\u53d1\u73b0\u662f\u5b58\u5728\u67d0\u4e2a\u5bc4\u5b58\u5668\u6765\u63a7\u5236\u662f\u5426\u62a5\u5f02\u5e38<\/p>\n<h2>3.sleep<\/h2>\n<blockquote><p>** \u5728Linux\u73af\u5883\u4e0b\u7f16\u5199\u4e00\u4e2a\u53ef\u4ee5\u7761\u77205\u79d2\u540e\u6253\u5370\u51fa\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u628a\u5b57\u7b26\u4e32\u5185\u5bb9\u5b58\u5165\u4e00\u4e2a\u6587\u4ef6\u4e2d\u7684\u5e94\u7528\u7a0b\u5e8fA\u3002(\u57fa\u4e8eC\u6216Rust\u8bed\u8a00)<\/p><\/blockquote>\n<p>\u5b66\u4e60\u4e86\u4e00\u70b9rust\u4e2d\u7684\u6587\u4ef6\u64cd\u4f5c\uff0c\u5e76\u5b66\u4e60\u4e86\u4e00\u70b9\u9519\u8bef\u5904\u7406\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u5f97\u5230\u4e86\u5982\u4e0b\u6700\u4f73\u5b9e\u8df5<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u9884\u6599\u4e4b\u5185\u7684\u9519\u8bef\u5e94\u8be5\u9519\u8bef\u4f20\u64ad\uff0c\u5982rust\u4e2d\u7684\uff1f<\/li>\n<li>\u5bf9\u4e8e\u9884\u6599\u4e4b\u5916\u7684\u9519\u8bef\u5e94\u8be5\u7acb\u523bpanic\u5e76\u9000\u51fa<\/li>\n<li>\u51fd\u6570\u5e94\u8be5\u68c0\u67e5\u53c2\u6570\u7684\u5408\u6cd5\u6027\uff0c\u6700\u597d\u5c06\u53c2\u6570\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u65b0\u7c7b\u578b\uff0c\u5e76\u7531\u7c7b\u578b\u68c0\u67e5\u6765\u68c0\u67e5\u5408\u6cd5\u6027<\/li>\n<li>\u4f7f\u7528unwrap\u7b49\u65b9\u6cd5\u4f1a\u6bd4match\u66f4\u7b80\u6d01<\/li>\n<\/ul>\n<p>\u4e4b\u540e\u7684\u4ee3\u7801<\/p>\n<pre><code class=\"language-rust\">use std::io::{self, Write};\nuse std::{thread, time};\nuse std::fs::{OpenOptions};\n\nfn main() -&gt; Result&lt;(), io::Error&gt;{\n    let mut file = OpenOptions::new()\n        .read(true)\n        .write(true)\n        .create(true)\n        .truncate(true)\n        .open(\"foo.txt\").expect(\"main: open file Unexpected error\");\n    loop {\n        file.write_all(b\"Hello, Rustn\").expect(\"main: write file unexpected error\");\n        println!(\"Hello, Rust!n\");\n        thread::sleep(time::Duration::new(5, 0));\n    }\n<\/code><\/pre>\n<h2>4.Concurrency<\/h2>\n<blockquote><p>*** \u5728Linux\u73af\u5883\u4e0b\u7f16\u5199\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8fB\uff0c\u7b80\u8981\u8bf4\u660e\u6b64\u7a0b\u5e8f\u80fd\u591f\u4f53\u73b0\u64cd\u4f5c\u7cfb\u7edf\u7684\u5e76\u53d1\u6027\u3001\u5f02\u6b65\u6027\u3001\u5171\u4eab\u6027\u548c\u6301\u4e45\u6027\u3002(\u57fa\u4e8eC\u6216Rust\u8bed\u8a00)<\/p><\/blockquote>\n<p>\u6709\u70b9\u61f5\u903c\uff0c\u53bb\u5b66\u4e86rust book\u7684<a href=\"https:\/\/doc.rust-lang.org\/book\/ch16-00-concurrency.html#fearless-concurrency\">Fearless Concurrency<\/a> \u4f46\u662f\u7b2c\u4e09\u4e2a\u6709\u70b9\u770b\u4e0d\u61c2\uff0c\u5c31\u662f\u7ebf\u7a0b\u4e2d\u901a\u8fc7\u5185\u5b58\u5171\u4eab\u6765\u901a\u4fe1\uff0c\u56e0\u4e3a\u8fd8\u6ca1\u770b\u667a\u80fd\u6307\u9488\u90e8\u5206\uff0c\u7136\u540e\u5199\u4e0b\u4e86\u5982\u4e0b\u7b80\u5355\u7684\u7ebf\u7a0b\u4ee3\u7801<\/p>\n<pre><code class=\"language-rust\">use std::{sync::mpsc, thread, time::Duration};\n\nfn main() {\n    let (tx, rx) = mpsc::channel();\n    let handle = thread::spawn(move || {\n        for i in 1..10 {\n            println!(\"hi {i} from spawned thread!\");\n            \/\/thread::sleep(Duration::from_millis(1));\n        }\n        tx.send(String::from(\"hi\")).unwrap();\n    });\n    let received = rx.recv().unwrap();\n    for i in 1..10 {\n        println!(\"hi {i} from main thread!\");\n        \/\/thread::sleep(Duration::from_millis(1));\n    }\n    println!(\"got {received}\");\n    handle.join().unwrap();\n}<\/code><\/pre>\n<p>\u5c06<code>handle.join().unwrap();<\/code> \u548c <code>let received = rx.recv().unwrap();<\/code>\u653e\u5728\u4e0d\u540c\u4f4d\u7f6e\u4f1a\u6253\u5370\u51fa\u4e0d\u540c\u7684\u7ed3\u679c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ch0 Anwser 1.\u914d\u73af\u5883 \u6211\u7528\u7684\u662fnixos\/mac&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56,11],"tags":[],"class_list":["post-5282","post","type-post","status-publish","format-standard","hentry","category-rcore","category-cs"],"_links":{"self":[{"href":"https:\/\/www.yizishun.com\/index.php?rest_route=\/wp\/v2\/posts\/5282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yizishun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yizishun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yizishun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yizishun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5282"}],"version-history":[{"count":2,"href":"https:\/\/www.yizishun.com\/index.php?rest_route=\/wp\/v2\/posts\/5282\/revisions"}],"predecessor-version":[{"id":5284,"href":"https:\/\/www.yizishun.com\/index.php?rest_route=\/wp\/v2\/posts\/5282\/revisions\/5284"}],"wp:attachment":[{"href":"https:\/\/www.yizishun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yizishun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yizishun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}