doc: about rust content (#70)
This commit is contained in:
parent
84876edb2e
commit
ada64ac643
130
docs/rust.md
130
docs/rust.md
@ -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
|
### Hello_World.rs
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
@ -981,9 +1010,108 @@ fn dead_end() -> ! {
|
|||||||
panic!("panic!!!!!");
|
panic!("panic!!!!!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--rehype:className=wrap-text -->
|
<!--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();
|
||||||
|
```
|
||||||
|
|
||||||
杂项
|
杂项
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user