From a824b932d8409574606f147e03b039f5050510f4 Mon Sep 17 00:00:00 2001 From: egg12138 Date: Sat, 27 Apr 2024 02:09:37 +0800 Subject: [PATCH] =?UTF-8?q?2024:=20Egg12138=20=E7=AC=AC=E4=B8=80=E9=98=B6?= =?UTF-8?q?=E6=AE=B5=20=E6=80=BB=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...65\346\200\273\347\273\223FromEgg12138.md" | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 "source/_posts/2024\344\270\200\351\230\266\346\256\265\346\200\273\347\273\223FromEgg12138.md" diff --git "a/source/_posts/2024\344\270\200\351\230\266\346\256\265\346\200\273\347\273\223FromEgg12138.md" "b/source/_posts/2024\344\270\200\351\230\266\346\256\265\346\200\273\347\273\223FromEgg12138.md" new file mode 100644 index 00000000000..e051c11c9ce --- /dev/null +++ "b/source/_posts/2024\344\270\200\351\230\266\346\256\265\346\200\273\347\273\223FromEgg12138.md" @@ -0,0 +1,65 @@ +--- +title: 2024一阶段总结FromEgg12138 +date: 2024-04-27 01:41:26 +tags: + - author:Egg12138 +--- + +## 阶段一 Rust复习 + +因为本科期间用 Rust2021 比用c++20更多,所以对Rust的基本用法和tricks比较熟。所以最近时间更多用来做毕设。快到一阶段DDL时开始做 rustlings. 和 quiz. + + +1. `as_mut`, `as_ptr`, `as_ref`, 以及rust2021引入的`as_deref` ,源码虽然都是一行,但是还是要注意很多细节的: + +```rust + pub const unsafe fn as_mut<'a>(&mut self) -> &'a mut T { + unsafe { &mut *self.as_ptr() } + + } + pub const unsafe fn as_ref<'a>(&self) -> &'a T { + unsafe { &*self.as_ptr().cast_const() } + // 实际上就是: + // unsafe {&*self.as_ptr() as *const T} + } + + pub const fn as_ptr(self) -> *mut T { + self.pointer as *mut T + } + +/// 该函数是Rust中是builtin的,命名为`cast_const`,它用于在不改变类型的情况下改变常量性(constness)。 该函数接受一个指向类型`T`的常量指针`self`作为输入,并返回一个指向类型`T`的常量指针。 它不会在代码重构时默默地改变类型。此外,虽然在大多数情况下,`*mut T`类型可以自动转换为`*const T`类型,但为了与`*const T`上的`cast_mut`函数保持对称性,所以该函数仍然需要(GPT)。 该函数是`const`函数,可以在编译时常量上下文中被评估,也是`inline`函数,总是会被内联到调用处。 该函数的返回类型是`*const T`,其中`T`是函数接受的指针类型`self`所指向的类型。 + pub const fn cast_const(self) -> *const T { + self as _ + } +``` + +2. 重新复习了 Rust 的异步原语和基本的 tokio runtime. + +3. 对于 智能指针的 `leak` 方法,之前用的还挺少的。 + +4. 参考 `dbg!` 写了一个 `func_name`, 使用它来做一些debug,在 `no_std` 下可能不能正常使用(还没试,有空到二阶段再 + +```rust +#[macro_export] +macro_rules! func_name { + () => {{ + fn f() {} + fn type_name_of(_: T) -> &'static str { + core::any::type_name::() + } + let name = type_name_of(f); + name.strip_suffix("::f").unwrap() + }}; +} +``` + +5. 对lifetime的理解一直都不是那么到位。这次稍微注意了一下`T: 'static`, `&'static T`的差异 + +6. Rust 的 FP 还是很舒服的。 + +7. rustlings的 `algo1.rs` 那道题,没有想到用 `safe` 的方式怎么实现。那道题里面都是 `unsafe`, 还在卡顿中。而且我用了 `NonNull::read_volatile`, 很危险。 + +没有太多写的总结。一方面是不是很有空,另一方面是这算是一个复习。 + +二阶段会记录更多的东西。之前之做过 xv6 的labs,因为是时间问题,没有做 JYY OSLab 和 PA我一直觉得很遗憾。作为一个物理学学生,肯定是希望能在二阶段学到更多有趣的东西的。 +