Skip to content

Commit 6d45b50

Browse files
ehusstraviscross
authored andcommitted
2024: Document reserving gen keyword
1 parent 64dda42 commit 6d45b50

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@
4747
- [Cargo: Table and key name consistency](rust-2024/cargo-table-key-names.md)
4848
- [Cargo: Reject unused inherited default-features](rust-2024/cargo-inherited-default-features.md)
4949
- [Rustfmt: Combine all delimited exprs as last argument](rust-2024/rustfmt-overflow-delimited-expr.md)
50+
- [`gen` keyword](rust-2024/gen-keyword.md)

src/rust-2024/gen-keyword.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# `gen` keyword
2+
3+
🚧 The 2024 Edition has not yet been released and hence this section is still "under construction".
4+
More information may be found in the tracking issue at <https://github.com/rust-lang/rust/issues/123904>.
5+
6+
## Summary
7+
8+
- `gen` is a [reserved keyword].
9+
10+
[reserved keyword]: ../../reference/keywords.html#reserved-keywords
11+
12+
## Details
13+
14+
The `gen` keyword has been reserved as part of [RFC #3513] to introduce "gen blocks" in a future release of Rust. `gen` blocks will provide a way to make it easier to write certain kinds of iterators. Reserving the keyword now will make it easier to stabilize `gen` blocks before the next edition.
15+
16+
[RFC #3513]: https://rust-lang.github.io/rfcs/3513-gen-blocks.html
17+
18+
## Migration
19+
20+
Introducing the `gen` keyword can cause a problem for any identifiers that are already called `gen`. For example, any variable or function name called `gen` would clash with the new keyword. To work around this issue, Rust supports the `r#` prefix for a [raw identifier], which allows identifiers to overlap with keywords.
21+
22+
The [`keyword_idents_2024`] lint will automatically modify any identifier named `gen` to be `r#gen` so that the code continues to work on both editions. This lint is part of the `rust-2024-compatibility` lint group, which will automatically be applied when running `cargo fix --edition`. To migrate your code to be Rust 2024 Edition compatible, run:
23+
24+
```sh
25+
cargo fix --edition
26+
```
27+
28+
For example, this will change:
29+
30+
```rust
31+
fn gen() {
32+
println!("generating!");
33+
}
34+
35+
fn main() {
36+
gen();
37+
}
38+
```
39+
40+
to be:
41+
42+
```rust
43+
fn r#gen() {
44+
println!("generating!");
45+
}
46+
47+
fn main() {
48+
r#gen();
49+
}
50+
```
51+
52+
Alternatively, you can manually enable the lint to find places where the `gen` identifiers need to be modified to be `r#gen`:
53+
54+
```rust
55+
// Add this to the root of your crate to do a manual migration.
56+
#![warn(keyword_idents_2024)]
57+
```
58+
59+
[raw identifier]: ../../reference/identifiers.html#raw-identifiers
60+
[`keyword_idents_2024`]: ../../rustc/lints/listing/allowed-by-default.html#keyword-idents-2024

0 commit comments

Comments
 (0)