Skip to content

Commit

Permalink
doc: about rust content (jaywcjlove#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwqaaq authored Nov 15, 2022
1 parent 84876ed commit ada64ac
Showing 1 changed file with 129 additions and 1 deletion.
130 changes: 129 additions & 1 deletion docs/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@ Rust 快速参考备忘单,旨在为编写基本语法和方法提供帮助。
入门
---

### 配置 vscode 调试
<!--rehype:wrap-class=row-span-2-->

参考:<https://github.com/vadimcn/vscode-lldb/blob/master/MANUAL.md#source-path-remapping>

下载 CodeLLDB,选择 rust 自动生成 launch.json 文件

```json
{
"configurations": [
// 添加一下行,使 vec、hashmap 等类型显示正常
"sourceLanguages": ["rust"]
]
}
```

----

将编译文件与标准库的位置进行映射

```json
{
"lldb.launch.sourceMap": {
// 你自己的映射 hash 和映射路径
"/rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f": "/Users/feiwu/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust"
}
}
```

### Hello_World.rs

```rust
Expand Down Expand Up @@ -981,9 +1010,108 @@ fn dead_end() -> ! {
panic!("panic!!!!!");
}
```

<!--rehype:className=wrap-text -->

惯用转换
-----

### &str -> String

```rust
String::from("str");
"str".to_string();
"str".to_owned();
```

### &str -> &[u8]

```rust
"str".as_bytes();
```

或者你也可以使用 `b""`

```rust
println!("{:?}", b"str");
```

### &str -> Vec<u8>

```rust
"str".as_bytes().to_vec();
"str".as_bytes().to_owned();
```

### String -> &str

```rust
let s = String::from("str");
let r = s.as_str();
```

### String -> &[u8]

```rust
let s = String::from("str");
let v = s.as_bytes();
```

### String -> Vec<u8>

```rust
let s = String::from("str");
let v = s.into_bytes();
```

### &[u8] -> &str

```rust
let b = "str".as_bytes();
let str = std::str::from_utf8(b).unwrap();
```

### &[u8] -> String

```rust
let b = "str".as_bytes();
let str = String::from_utf8(b.to_vec()).unwrap();
```

### &[u8] -> Vec<u8>

```rust
let b = "str".as_bytes();
let str = b.to_vec();
```

----

```rust
let b = "str".as_bytes();
let str = b.to_owned();
```

### Vec<u8> -> &str

```rust
let b = "str".as_bytes().to_vec();
let s = std::str::from_utf8(&b).unwrap();
```

### Vec<u8> -> &[u8]

```rust
let b = "str".as_bytes().to_vec();
let s = b.as_slice();
```

### Vec<u8> -> String

```rust
let b = "str".as_bytes().to_vec();
let s = String::from_utf8(b).unwrap();
```

杂项
-----

Expand Down

0 comments on commit ada64ac

Please sign in to comment.