Skip to content

Commit

Permalink
update 2024年 12月 21日 星期六 13:33:42 CST
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrysisa committed Dec 21, 2024
1 parent 0bf642f commit 33ba3d1
Show file tree
Hide file tree
Showing 53 changed files with 387 additions and 515 deletions.
3 changes: 1 addition & 2 deletions content/posts/cpp/atomic-weapons.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ tags:
- Concurrency
- C++
categories:
- C++
- Concurrency
- CPP
hiddenFromHomePage: false
hiddenFromSearch: false
hiddenFromRss: false
Expand Down
2 changes: 1 addition & 1 deletion content/posts/cpp/cpp-class-static-reflection.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tags:
- C++
- Reflection
categories:
- C++
- CPP
hiddenFromHomePage: false
hiddenFromSearch: false
hiddenFromRss: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tags:
- C++
- MSVC
categories:
- C++
- CPP
hiddenFromHomePage: false
hiddenFromSearch: false
hiddenFromRss: false
Expand Down
41 changes: 16 additions & 25 deletions content/posts/go/lets-go.md → content/posts/go/tgpl.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Let's Go"
title: "The Go Programming Language"
subtitle:
date: 2024-03-25T10:16:26+08:00
# draft: true
Expand Down Expand Up @@ -38,6 +38,12 @@ repost:
# See details front matter: https://fixit.lruihao.cn/documentation/content-management/introduction/#front-matter
---

{{< image src="https://go.dev/images/gophers/ladder.svg" caption="Gopher" >}}

{{< center-quote >}}
**Build simple, secure, scalable systems with Go**
{{< /center-quote >}}

<!--more-->

## A Tour of Go
Expand Down Expand Up @@ -742,6 +748,8 @@ Read the relevant blogs:
- [Defer, Panic, and Recover](https://go.dev/blog/defer-panic-and-recover)
- [Go Slices: usage and internals](https://go.dev/blog/slices-intro)

{{< admonition quote "Where to Go from here..." >}}

You can get started by [installing Go](https://go.dev/doc/install/).

Once you have Go installed, the [Go Documentation](https://go.dev/doc/) is a great place to continue. It contains references, tutorials, videos, and more.
Expand All @@ -760,35 +768,18 @@ The [Go Blog](https://go.dev/blog/) has a large archive of informative Go articl

Visit [the Go home page](https://go.dev/) for more.

## Building Microservices with Go

YouTube: [Building Microservices with Go](https://www.youtube.com/playlist?list=PLmD8u-IFdreyh6EUfevBcbiuCKzFk0EW_)
{{< /admonition >}}

> Week by week Building Microservices builds on the previous weeks code teaching you how to build a multi-tier microservice system. The code structure for the course is one of a mono repo. To make it simple to follow along, each episode has its own branch showing progress to date.
### Setup
## Project: Building Microservices with Go

Linux: WLS2
YouTube: Nic Jackson 制作的 [Building Microservices with Go](https://www.youtube.com/playlist?list=PLmD8u-IFdreyh6EUfevBcbiuCKzFk0EW_) 系列影片

```bash
$ uname --a
Linux cai-PC 5.15.77-amd64-desktop #2 SMP Thu Jun 15 16:06:18 CST 2023 x86_64 GNU/Linux
```

VS Code and extensions:
- Go
- Docker
- Live Share

Go:

```bash
$ go version
go version go1.22.5 linux/amd64
```
> Week by week Building Microservices builds on the previous weeks code teaching you how to build a multi-tier microservice system. The code structure for the course is one of a mono repo. To make it simple to follow along, each episode has its own branch showing progress to date.
## References:

- Matt KØDVB: [Go Class](https://www.youtube.com/playlist?list=PLoILbKo9rG3skRCj37Kn5Zj803hhiuRK6): 作为深入理解 Go 语言机制的参考材料
- [A tour of Go](https://go.dev/tour/list)
- Matt KØDVB\'s [Go Class](https://www.youtube.com/playlist?list=PLoILbKo9rG3skRCj37Kn5Zj803hhiuRK6): 作为深入理解 Go 语言机制的参考材料
- Nic Jackson\'s [Building Microservices with Go](https://www.youtube.com/playlist?list=PLmD8u-IFdreyh6EUfevBcbiuCKzFk0EW_): 一个不错的 Go 练习项目
- [Learn Go with Tests](https://quii.gitbook.io/learn-go-with-tests)
- [Go by Example](https://gobyexample.com/)
2 changes: 1 addition & 1 deletion content/posts/linux-kernel-internals/linux-quiz1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "Linux 核心设计: 第 1 周测验题 linked list"
subtitle:
date: 2024-02-16T14:59:25+08:00
# draft: true
draft: true
# author:
# name:
# link:
Expand Down
2 changes: 1 addition & 1 deletion content/posts/linux-kernel-internals/linux2023-lab0.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "Linux 核心设计: lab0-c"
subtitle:
date: 2024-02-19T16:23:01+08:00
# draft: true
draft: true
# author:
# name:
# link:
Expand Down
2 changes: 1 addition & 1 deletion content/posts/linux-kernel-internals/linux2023-quiz1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "Linux 核心设计: 2023q1 第一周测验题"
subtitle:
date: 2024-06-07T10:33:35+08:00
# draft: true
draft: true
# author:
# name:
# link:
Expand Down
2 changes: 1 addition & 1 deletion content/posts/linux-kernel-internals/linux2023-quiz2.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "Linux 核心设计: 2023q2 第二周测验题"
subtitle:
date: 2024-06-08T10:52:10+08:00
# draft: true
draft: true
# author:
# name:
# link:
Expand Down
2 changes: 1 addition & 1 deletion content/posts/math/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: 数学开放式课程学习指引
subtitle:
date: 2023-12-23T21:35:47+08:00
draft: false
draft: true
author:
name: vanJker
link: https://github.com/vanJker
Expand Down
2 changes: 1 addition & 1 deletion content/posts/math/probability-and-statistics.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: "Probability and Statistics"
subtitle:
date: 2024-01-20T17:20:09+08:00
slug: 7592e7c
# draft: true
draft: true
author:
name: vanJker
link: https://github.com/vanJker
Expand Down
3 changes: 0 additions & 3 deletions content/posts/riscv/oerv-pretask.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ tags:
- RISC-V
- openEuler
- QEMU
- Neofetch
- Container
- chroot
- nspawn
- Architecture
categories:
- RISC-V
Expand Down
73 changes: 73 additions & 0 deletions content/posts/rust/demystify-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ repost:

{{< image src="/images/rust/rust.png" caption="Crustacean" >}}

{{< center-quote >}}
**A language empowering everyone to build reliable and efficient software.**
{{< /center-quote >}}

<!--more-->

## 基础入门
Expand All @@ -52,6 +56,8 @@ repost:
为何不用赋值而用绑定呢(其实你也可以称之为赋值,但是绑定的含义更清晰准确)?这里就涉及 Rust 最核心的原则——所有权,简单来讲,任何内存对象都是有主人的,而且一般情况下完全属于它的主人,绑定就是把这个对象绑定给一个变量,让这个变量成为它的主人(聪明的读者应该能猜到,在这种情况下,该对象之前的主人就会丧失对该对象的所有权),像极了我们的现实世界,不是吗?
{{< /admonition >}}

> 即将变量名与某块内存区域进行 **绑定**
变量作用域本质上是栈帧,所以可以实现类似函数调用的变量遮蔽。但与栈帧有一点不同,它可以访问作用域外面的数据,而栈帧则不行。这是因为每一次函数调用的上文可能不同(调用者不同导致上文不同),所以不能安全地访问栈帧外的数据,而变量作用域的上文是固定的(变量作用域常出现于函数内部,即作用域之前的语句都是固定的),所以可以安全地访问作用域外部数据(这一点倒是和全局变量类似,毕竟对于函数调用来说全局变量是可以确定的上文)。当然变量遮蔽会导致无法访问作用域外部的同名变量。

### 基本类型
Expand All @@ -71,9 +77,76 @@ let a = let x = 1 + 2;; // compile error

Rust 的函数名和变量名采用蛇形命名法 (snake case),例如 `fn add_two(x: i32, y: i32) -> i32`

## 所有权和借用

Rust 的所有权系统为其提供了高性能的内存安全方案,而不是垃圾回收机制 (GC) 这种性能稍有欠缺的内存安全方案。这是因为所有权机制检查只发生在编译期,因此对于程序运行期,不会有任何性能上的损失。

栈 (stack) 与堆 (heap) 灵活性的差异在与:栈的操作方式过于单一,只能后进先出,不能灵活使用空闲空间;而堆支持任意访问 (random access),所以可以灵活使用空闲空间,但也导致堆是缺乏组织的结构,而 Rust 的所有权机制就是对这一缺乏组织的结构采用类似栈的方式(作用域判定)进行检查管理。但堆的数据需要被栈上数据所引用才能被程序所使用(需要先索引才能使用),因而让栈上的数据都被所有权系统认为是堆的索引,导致了性能问题,从而引出了 `Copy` 这个 Trait。

```rs
struct StringView {
data: String,
};
let x: StringView = {
data: String::from("Hello"),
};
let y = x; // move ownership from x to y, x cann't be accessed!


struct StringView {
data: i32,
};
let x: StringView = {
data: 32,
};
let y = x; // also move ownership from x to y, x cann't be accessed!


#[derive(Copy)]
struct StringView {
data: i32,
};
let x: StringView = {
data: 32,
};
let y = x; // copy x to y, both x and y can be accessed!
```

不可变引用 (Immutable Reference) 类型 `&T` 都实现了 `Copy` Trait,这符合实际,因为引用并没有拥有所有权,并且允许同时存在多个不可变引用(以优化为并行处理)。注意当所有权转移时,可变性也可以随之改变,毕竟有所有权可以想怎么变就怎么变。当变量中一部分的所有权被转移给其它变量时,部分 move 就会发生。在这种情况下,原变量将无法再被使用(因为它不再拥有对整体的所有权),但是它没有转移所有权的那一部分依然可以使用(因为它还拥有这一部分的所有权)。

{{< admonition >}}

个人观点:Rust 拥有三大类型

1. 有索引其它数据,绑定会移动所有权的类型 (e.g. `String`, `Vector` 等集合类型)
2. 没有索引其它数据,绑定执行拷贝语义的类型 (e.g. `i32`, `f64` 等基本类型)
3. 有索引其它数据,但绑定执行拷贝语义的类型 (e.g. `&T` 这类不可变引用类型)

自定义的结构体类型默认是上面的第一种类型(无论你的成员是否有引用其它数据),可以使用 `Copy` Trait 来将其行为变为第二种类型。元组 (tuple) 类型则默认是上面的第二种类型,除非元组成员有包含第一种类型,此时则会转变为第一种类型的行为。可变引用虽然没有所有权,但因为同时只能存在一个,所以行为与第一种类型一致。

{{< /admonition >}}

借用和引用(包括不可变引用和可变引用)针对的是上面的第一种类型,因为其他两种类型默认行的是拷贝而不是转移所有权,转移所有权的代价太大了需要一种轻量级的替代方案。Rust 的内存安全的一个方面体现在,它不会编译通过存在数据竞争的代码,即一个作用域存在多个可变引用或同时存在可变引用和不可变引用的代码,这种代码是不符合 Rust 的语法规范的(因为这种代码没有同步机制来防止未定义行为)。Rust 的编译器优化后 ([Non-Lexical Lifetimes (NLL)](https://blog.rust-lang.org/2022/08/05/nll-by-default.html)),引用作用域的结束位置从花括号(与变量相同)变成引用最后一次被使用的位置,这极大便利了引用在程序中的使用。引用与指针不同,它没有空的状态,必须总是有效的(索引了某个东西才能叫引用嘛)。

`ref``&` 类似,可以用来获取一个值的引用,但是它们的用法有所不同,e.g. `let ref r = x;` 等价于 `let r = &x`,可变引用的话加一个 `mut` 修饰即可。`ref` 常用于模式匹配中获取结构体的特定成员的引用。

## References

- [Rust 语言圣经 (Rust Course)](https://course.rs/)
- bilibili: [Golden Rust](https://space.bilibili.com/504069720/channel/collectiondetail?sid=3642485)
- [Programming Rust, 2nd Edition](https://www.oreilly.com/library/view/programming-rust-2nd/9781492052586/)
- Cliffle: [Learn Rust the Dangerous Way](https://cliffle.com/p/dangerust/)
- [The Rustonomicon](https://doc.rust-lang.org/nomicon/)
- [CodeCrafters](https://app.codecrafters.io/)
- [Rust Playground](https://play.rust-lang.org/)
- [Command line apps in Rust](https://rust-cli.github.io/book/index.html)

---

{{< details "待整理" >}}

## CLI in Rust

C 语言的 CLI 程序处理参数的逻辑是过程式的,即每次执行都会通过 `argv` 来获取本次执行的参数并进行相应的处理 (Rust 的 `std::env::args()` 处理 CLI 程序的参数方式也类似,都是对每次执行实例进行过程式的处理),而 [Clap](https://docs.rs/clap/latest/clap/) 不同,它类似于面向对象的思想,通过定义一个结构体 (object),每次运行时通过 `clap::Parser::parse` 获取并处理本次运行的参数 (即实例化 object),这样开发的 CLI 程序扩展性会更好。

{{< /details >}}
2 changes: 1 addition & 1 deletion content/posts/rust/drop-check.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ comment: false
weight: 0
tags:
- Rust
- Drop
- Drop Check
categories:
- Rust
hiddenFromHomePage: false
Expand Down
2 changes: 1 addition & 1 deletion content/posts/rust/impl-tcp-in-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "Impl Rust: TCP/IP"
subtitle:
date: 2024-02-17T19:01:53+08:00
# draft: true
draft: true
# author:
# name:
# link:
Expand Down
92 changes: 0 additions & 92 deletions content/posts/rust/rust-cli.md

This file was deleted.

2 changes: 1 addition & 1 deletion content/posts/rust/trpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,7 @@ pub fn some_name(input: TokenStream) -> TokenStream {

### Final Project: Web Server

## Practices
## Exercises

- [x] [rustlings](https://github.com/rust-lang/rustlings) / [参考题解](https://space.bilibili.com/50713701/channel/collectiondetail?sid=1409071) / [我的题解](https://github.com/ccrysisa/rustlings)
- [ ] [LifetimeKata](https://github.com/tfpk/lifetimekata/)
Expand Down
Loading

0 comments on commit 33ba3d1

Please sign in to comment.