mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2024-12-30 23:10:30 +08:00
Update Ch21
This commit is contained in:
parent
0904c7df52
commit
84a4ff9e22
8
raw_idenitifers/Cargo.toml
Normal file
8
raw_idenitifers/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "raw_idenitifers"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
7
raw_idenitifers/src/main.rs
Normal file
7
raw_idenitifers/src/main.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fn r#match(needle: &str, haystack: &str) -> bool {
|
||||||
|
haystack.contains(needle)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert! (r#match("foo", "foobar"));
|
||||||
|
}
|
@ -69,6 +69,44 @@
|
|||||||
- `virtual`
|
- `virtual`
|
||||||
- `yield`
|
- `yield`
|
||||||
|
|
||||||
### <a id="raw-pointers"></a>原始标识符
|
### <a id="raw-identifiers"></a>原始标识符
|
||||||
|
|
||||||
|
*原始标识符,raw identifiers* 属于允许实现使用一般不被允许关键字的语法。是通过在关键字前加上前缀 `r#`,使用原始标识符的。
|
||||||
|
|
||||||
|
比如,`match` 是个关键字。在咱们尝试编译下面这个使用 `match` 作其名字的函数时:
|
||||||
|
|
||||||
|
文件名:`src/main.rs`
|
||||||
|
|
||||||
|
```rust
|
||||||
|
fn match(needle: &str, haystack: &str) -> bool {
|
||||||
|
haystack.contains(needle)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
咱们将得到这样的报错:
|
||||||
|
|
||||||
|
```console
|
||||||
|
error: expected identifier, found keyword `match`
|
||||||
|
--> src/main.rs:1:4
|
||||||
|
|
|
||||||
|
1 | fn match(needle: &str, haystack: &str) -> bool {
|
||||||
|
| ^^^^^ expected identifier, found keyword
|
||||||
|
```
|
||||||
|
|
||||||
|
该报错显示咱们无法将关键字 `match` 用作函数标识符。要将 `match` 用作函数名字,咱们就需要使用原始标识符语法,像下面这样:
|
||||||
|
|
||||||
|
文件名:`src/main.rs`
|
||||||
|
|
||||||
|
```rust
|
||||||
|
fn r#match(needle: &str, haystack: &str) -> bool {
|
||||||
|
haystack.contains(needle)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert! (r#match("foo", "foobar"));
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
此代码将不带任何错误地编译。请注意那个函数的定义中,与 `main` 中该函数被调用处其名字上的 `r#` 前缀。
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user