Skip to content

Commit b4180be

Browse files
committed
mv rust from superLish/CS-Notes.
1 parent ba867a3 commit b4180be

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+4391
-1
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ Cargo.lock
88

99
# These are backup files generated by rustfmt
1010
**/*.rs.bk
11+
12+
.idea/
13+
.vscode/
14+
15+
**/.idea
16+
**/.vscode
17+
**/target/

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,33 @@
11
# my-rust
2-
personal rust study notes
2+
personal rust study notes.
3+
4+
### 异步
5+
- [Rust异步之Future](./async/Rust异步之Future.md)
6+
- [Rust异步之tokio](./async/Rust异步之tokio.md)
7+
8+
9+
### 其他
10+
- [Rust学习资料汇总](./Rust/Rust学习资料汇总.md)
11+
- [RefCell和内部可变性](./Rust/RefCell和内部可变性.md)
12+
- [Rust关联类型与默认类型参数](./Rust/Rust关联类型与默认类型参数.md)
13+
- [Rust写时复制](./Rust/Rust写时复制.md)
14+
- [Rust完全限定语法与消歧义:调用相同名称的方法](./Rust/Rust完全限定语法与消歧义:调用相同名称的方法.md)
15+
- [Rust更换Crates源](./Rust/Rust更换Crates源)
16+
- [Rust生命周期bound用于泛型的引用](./Rust/Rust生命周期bound用于泛型的引用.md)
17+
- [对Rust所有权、借用及生命周期的理解](./Rust/对Rust所有权、借用及生命周期的理解.md)
18+
- [Rust实现的常用密码学库](./Rust/Rust实现的常用密码学库)
19+
- [Rust中使用Protocol Buffers](./Rust/Rust中使用ProtocolBuffers.md)
20+
- [Rust中的panic宏](./Rust/Rust中的panic宏.md)
21+
- [线程池的简单实现(Rust)](./Rust/线程池的简单实现(Rust).md)
22+
- [记一次排查内存泄漏的过程](./Rust/记一次排查内存泄漏的过程.md)
23+
- [Rust轻量级I/O库mio](./Rust/Rust轻量级IO库mio.md)
24+
- [Rust双重循环break的问题](./Rust双重循环break的问题.md)
25+
- [log4rs日志库简析](./Rust/log4rs日志库简析.md)
26+
- [Rust关于ParticalEq与Eq](./Rust/cmp/particaleq/README.md)
27+
- [Rust中的Arc与Rc](./Rust/Rust中的Arc与Rc.md)
28+
29+
30+
### 个人编写的Rust程序
31+
32+
- [craft](https://github.com/superLish/craft)
33+
- [lru-simple](https://github.com/superLish/lru-simple)

Rust/RefCell和内部可变性.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#### RefCell
2+
Rust在编译阶段会进行严格的借用规则检查,规则如下:
3+
>- 在任意给定时间,要么只能有一个可变引用,要么只能有多个不可变引用。
4+
>- 引用必须总是有效。
5+
6+
即在编译阶段,当有一个不可变值时,不能可变的借用它。如下代码所示:
7+
```rust
8+
fn main() {
9+
let x = 5;
10+
let y = &mut x;
11+
}
12+
```
13+
会产生编译错误:
14+
```rust
15+
error[E0596]: cannot borrow immutable local variable `x` as mutable
16+
--> src/main.rs:32:18
17+
|
18+
31 | let x = 5;
19+
| - consider changing this to `mut x`
20+
32 | let y = &mut x;
21+
| ^ cannot borrow mutably
22+
```
23+
但是在实际的编程场景中可能会需要在有不可变引用时改变数据的情况,这时可以考虑Rust中的内部可变性。其借用规则检查由编译期推迟到运行期。对应的,在编译期借用规则检查不通过,则会产生编译错误;而运行期借用规则检查不通过,则会```panic```,且有运行期的代价。
24+
25+
所以实际代码中使用```RefCell<T>```的情况是当你确定你的代码遵循借用规则,而编译器不能理解和确定的时候。代码仍然要符合借用规则,只不过规则检查放到了运行期。
26+
27+
#### RefCell代码实例1:
28+
```rust
29+
use std::cell::RefCell;
30+
31+
fn main() {
32+
let x = RefCell::new(5u8);
33+
assert_eq!(5, *x.borrow());
34+
{
35+
let mut y = x.borrow_mut();
36+
*y = 10;
37+
assert_eq!(10, *x.borrow());
38+
let z = x.borrow(); //编译时会通过,但运行时panic!
39+
}
40+
}
41+
```
42+
运行结果:
43+
```rust
44+
thread 'main' panicked at 'already mutably borrowed: BorrowError', libcore/result.rs:983
45+
:5
46+
note: Run with `RUST_BACKTRACE=1` for a backtrace.
47+
```
48+
可以看到在运行时进行了借用检查,并且panic!
49+
50+
#### RefCell代码实例2:
51+
```rust
52+
#[derive(Debug, Default)]
53+
struct Data {
54+
a: u8,
55+
b: RefCell<u8>,
56+
}
57+
58+
impl Data {
59+
// 编译通过
60+
pub fn value_b(&self) -> u8 {
61+
let mut cache = self.b.borrow_mut();
62+
if *cache != 0 {
63+
return *cache;
64+
}
65+
*cache = 100;
66+
*cache
67+
}
68+
69+
//编译错误:cannot mutably borrow field of immutable binding
70+
pub fn value_a(&self) -> u8 {
71+
if self.a != 0 {
72+
return self.a;
73+
}
74+
75+
self.a = 100;
76+
self.a
77+
}
78+
}
79+
80+
fn main() {
81+
let value = Data::default();
82+
println!("{:?}", value);
83+
value.value_b();
84+
println!("{:?}", value);
85+
}
86+
87+
```
88+
把```value_a```注释掉运行结果如下:
89+
```rust
90+
Data { a: 0, b: RefCell { value: 0 } }
91+
Data { a: 0, b: RefCell { value: 100 } }
92+
```
93+
很多时候我们只能获取一个不可变引用,然而又需要改变所引用数据,这时用```RefCell<T>```是解决办法之一。
94+
95+
96+
#### 内部可变性
97+
98+
内部可变性(Interior mutability)是Rust中的一个设计模式,它允许你即使在有不可变引用时改变数据,这通常是借用规则所不允许的。为此,该模式在数据结构中使用unsafe代码来模糊Rust通常的可变性和借用规则。当可以确保代码在**运行时**会遵守借用规则,即使编译器不能保证的情况,可以选择使用那些运用内部可变性模式的类型。所涉及的 unsafe 代码将被封装进安全的 API 中,而外部类型仍然是不可变的。

Rust/Rust中使用ProtocolBuffers.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Google Protocol Buffer(简称 Protobuf)是一种轻便高效的结构化数据存储格式,平台无关、语言无关、可扩展,可用于通讯协议和数据存储等领域。下面给出在Rust中使用Protocol Buffers的基本流程步骤。
2+
3+
4+
下面以Ubuntu-16.04LTS为例:
5+
### 一、安装protoc
6+
7+
0.预先安装
8+
```
9+
sudo apt-get install autoconf automake libtool curl make g++ unzip
10+
```
11+
12+
1.获取源码,生成configure
13+
```
14+
git clone https://github.com/google/protobuf.git
15+
cd protobuf
16+
git submodule update --init --recursive
17+
./autogen.sh
18+
```
19+
20+
2.编译安装
21+
```
22+
./configure #By default, the package will be installed to /usr/local
23+
make
24+
make check
25+
sudo make install
26+
sudo ldconfig # refresh shared library cache.
27+
```
28+
>安装步骤可参考:https://github.com/google/protobuf/blob/master/src/README.md
29+
30+
### 二、安装protoc-gen-rust插件
31+
使用cargo 安装:
32+
```
33+
cargo install protobuf --vers 1.7.4 #1.7.4为版本号,可选填。默认安装到~/.cargo/bin目录中
34+
```
35+
36+
还可使用源码安装,从github上clone源码,编译安装,加入环境变量。安装步骤可参考:https://github.com/stepancheg/rust-protobuf/tree/master/protobuf-codegen
37+
38+
### 三、编写proto文件生成对应rust文件
39+
proto文件语法规则可参考:[Language Guide (proto3)](https://developers.google.com/protocol-buffers/docs/proto3)
40+
41+
举例说明(在当前目录下生成foo.proto对应的rust文件):
42+
```
43+
protoc --rust_out . foo.proto
44+
```
45+
如果是其他语言,可在[Third-Party Add-ons for Protocol Buffers](https://github.com/google/protobuf/blob/master/docs/third_party.md)中找相关语言的插件等。
46+
### 四、工程应用
47+
1. 在rust工程中Cargo.toml中的添加protobuf
48+
```toml
49+
[dependencies]
50+
protobuf = "1.7" //注意版本问题,1.x与2.x,同时这里的版本 须与上面安装的protobuf版本相一致
51+
```
52+
2. 添加引用的crate:
53+
```rust
54+
extern crate protobuf;
55+
```
56+
3. 引用相关api......
57+
58+
>学习文档: [Developer Guide](https://developers.google.com/protocol-buffers/docs/overview)

0 commit comments

Comments
 (0)