Skip to content

Commit

Permalink
Merge pull request #316 from Egg12138/master
Browse files Browse the repository at this point in the history
2024: Egg12138 第一阶段 总结
  • Loading branch information
ZhiyuanSue authored Apr 26, 2024
2 parents 0376c4c + a824b93 commit ca957f4
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions source/_posts/2024一阶段总结FromEgg12138.md
Original file line number Diff line number Diff line change
@@ -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>(_: T) -> &'static str {
core::any::type_name::<T>()
}
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我一直觉得很遗憾。作为一个物理学学生,肯定是希望能在二阶段学到更多有趣的东西的。

0 comments on commit ca957f4

Please sign in to comment.