Skip to content

Commit

Permalink
docs: update basic skill
Browse files Browse the repository at this point in the history
  • Loading branch information
MuYunyun committed Aug 11, 2023
1 parent e3bbc8a commit 7245d48
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions BasicSkill/Shorcuts/vscode.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ abbrlink: ues1uzcj
### VScode 中使用 markdown 不能显示预览图片

按 cmd + shift + p,输入 markdown,搜索 markdown 更改预览安全设置,选择 Allow Insecure Contents 即可。

### 性能报告

>Developer:Startup Performance
可用于查看各插件占用时间。
2 changes: 2 additions & 0 deletions BasicSkill/Shorcuts/易读错音的程序词汇.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ abbrlink: oh2njhvb

* cache: [美: kæʃ]
* babel: [美: 'beibəl]

> [chinese-programmer-wrong-pronunciation](https://github.com/shimohq/chinese-programmer-wrong-pronunciation)
70 changes: 70 additions & 0 deletions BasicSkill/rust/rust_note.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ abbrlink: 9oeefka1
- [常见的枚举类型:Option](#常见的枚举类型option)
- [控制流运算符 match](#控制流运算符-match)
- [简单控制流 if let](#简单控制流-if-let)
- [模式](#模式)
- [模块系统](#模块系统)
- [路径](#路径)
- [通用集合类型](#通用集合类型)
Expand All @@ -30,6 +31,7 @@ abbrlink: 9oeefka1
- [生命周期及其标注语法](#生命周期及其标注语法)
- [Rust 内置三条计算生命周期规则](#rust-内置三条计算生命周期规则)
- [智能指针](#智能指针)
- [并发](#并发)
- [Rust 开发中一些约定俗成原则](#rust-开发中一些约定俗成原则)
- [释义](#释义)

Expand Down Expand Up @@ -581,6 +583,12 @@ match EXPR {

2. if 与 if let 可以混合使用。

#### 模式

1. 模式分为不可失败(irrefutable)和可失败(refutable)两种类型。不可失败模式能够匹配任何传入的值,可失败模式则可能因为某些特定的值而匹配失败。
1. 函数参数、let 语句以及 for 循环只接收不可失败模式。
2. if let 和 while let 表达式则只接收可失败模式。

### 模块系统

1. 包(package)与单元包(crate)的区别是?
Expand Down Expand Up @@ -770,6 +778,68 @@ impl<T> Deref for MyBox<T> {

3. `RefCell<T>`: 通过内部可变性模式使我们可以修改一个不可变类型的内部值;它会在运行时而不是编译时承担起维护借用规则的责任。

### 并发

1. 创建新线程;

1.1. 使用 spawn 创建新线程;

```rust
use std::thread;
use std::time::Duration;

// hi number 1 from the main thread!
// hi number 1 from the spawned thread!
// hi number 2 from the main thread!
// hi number 2 from the spawned thread!
// hi number 3 from the main thread!
// hi number 3 from the spawned thread!
// hi number 4 from the main thread!
// hi number 4 from the spawned thread!
fn main() {
thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
thread::sleep(Duration::from_millis(1));
}
});

for i in 1..5 {
println!("hi number {} from the main thread!", i);
thread::sleep(Duration::from_millis(1));
}
}
```

1.2. 使用 join 句柄等待所有线程结束;

由于主线程停止,1.1 中案例在大部分情形下都会提前终止新线程,无法保证新线程一定会得到执行。

thread::spawn 的返回值类型是一个自持有所有权的 JoinHandle,调用它的 join 方法可以阻塞当前线程直达对应的新线程运行结束。从而保证新线程能在 main 函数退出前执行完毕。

```diff
fn main() {
+ let handle = thread::spawn(|| {
- thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
thread::sleep(Duration::from_millis(1));
}
});

+ handle.join().unwrap();

for i in 1..5 {
println!("hi number {} from the main thread!", i);
thread::sleep(Duration::from_millis(1));
}
}
```

2. 使用通道在线程间发送消息的传递式并发;
1. 关键字:`channel`
3. 允许多个线程访问同一片数据的共享状态式并发;

### Rust 开发中一些约定俗成原则

1. 将程序拆分为 main.rs 和 lib.rs,main.rs 负责运行程序,lib.rs 处理实际的逻辑。
Expand Down

0 comments on commit 7245d48

Please sign in to comment.