From 444b513260258e4fdddb55317dcb67ecea0198fe Mon Sep 17 00:00:00 2001 From: Dawn <2439646234@qq.com> Date: Tue, 19 Aug 2025 20:46:12 +0800 Subject: [PATCH] =?UTF-8?q?rust=E7=9A=84=E7=9D=A1=E7=9C=A0=E9=94=81?= =?UTF-8?q?=E5=92=8C=E8=87=AA=E6=97=8B=E8=AE=A1=E6=97=B6=E5=99=A8=EF=BC=8C?= =?UTF-8?q?=E8=A1=80=E5=8E=8B=E9=9A=8F=E7=9D=80=E8=AE=A1=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E5=8D=87=E9=AB=98=E8=80=8C=E5=8D=87=E9=AB=98=EF=BC=8C=E5=91=BC?= =?UTF-8?q?=E5=90=B8=E9=9A=8F=E7=9D=80=E7=9D=A1=E7=9C=A0=E9=94=81=E8=80=8C?= =?UTF-8?q?=E5=81=9C=E6=AD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/sleep_lock.rs | 15 +++++++++++++++ rust/timer.rs | 9 +++++++++ rust/unwrap.rs | 6 ++++++ 3 files changed, 30 insertions(+) create mode 100644 rust/sleep_lock.rs create mode 100644 rust/timer.rs create mode 100644 rust/unwrap.rs diff --git a/rust/sleep_lock.rs b/rust/sleep_lock.rs new file mode 100644 index 0000000..495e3f1 --- /dev/null +++ b/rust/sleep_lock.rs @@ -0,0 +1,15 @@ +/// 用睡眠当锁(假锁) +fn main() { + fn acquire_lock(path: &str) { + while std::path::Path::new(path).exists() { + std::thread::sleep(std::time::Duration::from_millis(200)); + } + std::fs::write(path, b"locked").ok(); + } + fn release_lock(path: &str) { let _ = std::fs::remove_file(path); } + + let lock = "/tmp/lock.file"; + acquire_lock(lock); + println!("临界区..."); + release_lock(lock); +} \ No newline at end of file diff --git a/rust/timer.rs b/rust/timer.rs new file mode 100644 index 0000000..8b5811a --- /dev/null +++ b/rust/timer.rs @@ -0,0 +1,9 @@ +/// 自旋“计时器”(CPU 风扇起飞)直接占满一个核心,不释放,🤣 +fn spin(ms: u64) { + let start = std::time::Instant::now(); + while start.elapsed().as_millis() < ms as u128 {} +} +fn main() { + spin(1000); + println!("OK"); +} \ No newline at end of file diff --git a/rust/unwrap.rs b/rust/unwrap.rs new file mode 100644 index 0000000..b819877 --- /dev/null +++ b/rust/unwrap.rs @@ -0,0 +1,6 @@ +/// 一步错步步错 +fn main() { + let port: u16 = std::env::var("PORT").unwrap().parse().unwrap(); + let cfg = std::fs::read_to_string("config.toml").unwrap(); + println!("PORT={port}, first={}", cfg.lines().next().unwrap()); +} \ No newline at end of file -- Gitee