Skip to content

Commit 96d710e

Browse files
プロジェクト内に存在しないファイルを英語版からコピー
1 parent d944d08 commit 96d710e

34 files changed

+3099
-0
lines changed

src/rust-2021/c-string-literals.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# C-string literals
2+
3+
## Summary
4+
5+
- Literals of the form `c"foo"` or `cr"foo"` represent a string of type [`&core::ffi::CStr`][CStr].
6+
7+
[CStr]: ../../core/ffi/struct.CStr.html
8+
9+
## Details
10+
11+
Starting with Rust 1.77, C-strings can be written using C-string literal syntax with the `c` or `cr` prefix.
12+
13+
Previously, it was challenging to properly produce a valid string literal that could interoperate with C APIs which terminate with a NUL byte.
14+
The [`cstr`] crate was a popular solution, but that required compiling a proc-macro which was quite expensive.
15+
Now, C-strings can be written directly using literal syntax notation, which will generate a value of type [`&core::ffi::CStr`][CStr] which is automatically terminated with a NUL byte.
16+
17+
```rust,edition2021
18+
# use core::ffi::CStr;
19+
20+
assert_eq!(c"hello", CStr::from_bytes_with_nul(b"hello\0").unwrap());
21+
assert_eq!(
22+
c"byte escapes \xff work",
23+
CStr::from_bytes_with_nul(b"byte escapes \xff work\0").unwrap()
24+
);
25+
assert_eq!(
26+
c"unicode escapes \u{00E6} work",
27+
CStr::from_bytes_with_nul(b"unicode escapes \xc3\xa6 work\0").unwrap()
28+
);
29+
assert_eq!(
30+
c"unicode characters αβγ encoded as UTF-8",
31+
CStr::from_bytes_with_nul(
32+
b"unicode characters \xce\xb1\xce\xb2\xce\xb3 encoded as UTF-8\0"
33+
)
34+
.unwrap()
35+
);
36+
assert_eq!(
37+
c"strings can continue \
38+
on multiple lines",
39+
CStr::from_bytes_with_nul(b"strings can continue on multiple lines\0").unwrap()
40+
);
41+
```
42+
43+
C-strings do not allow interior NUL bytes (such as with a `\0` escape).
44+
45+
Similar to regular strings, C-strings also support "raw" syntax with the `cr` prefix.
46+
These raw C-strings do not process backslash escapes which can make it easier to write strings that contain backslashes.
47+
Double-quotes can be included by surrounding the quotes with the `#` character.
48+
Multiple `#` characters can be used to avoid ambiguity with internal `"#` sequences.
49+
50+
```rust,edition2021
51+
assert_eq!(cr"foo", c"foo");
52+
// Number signs can be used to embed interior double quotes.
53+
assert_eq!(cr#""foo""#, c"\"foo\"");
54+
// This requires two #.
55+
assert_eq!(cr##""foo"#"##, c"\"foo\"#");
56+
// Escapes are not processed.
57+
assert_eq!(cr"C:\foo", c"C:\\foo");
58+
```
59+
60+
See [The Reference] for more details.
61+
62+
[`cstr`]: https://crates.io/crates/cstr
63+
[The Reference]: ../../reference/tokens.html#c-string-and-raw-c-string-literals
64+
65+
## Migration
66+
67+
Migration is only necessary for macros which may have been assuming a sequence of tokens that looks similar to `c"…"` or `cr"…"`, which previous to the 2021 edition would tokenize as two separate tokens, but in 2021 appears as a single token.
68+
69+
As part of the [syntax reservation] for the 2021 edition, any macro input which may run into this issue should issue a warning from the `rust_2021_prefixes_incompatible_syntax` migration lint.
70+
See that chapter for more detail.
71+
72+
[syntax reservation]: reserved-syntax.md

src/rust-2021/raw-lifetimes.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Raw lifetimes
2+
3+
## Summary
4+
5+
- `'r#ident_or_keyword` is now allowed as a lifetime, which allows using keywords such as `'r#fn`.
6+
7+
## Details
8+
9+
Raw lifetimes are introduced in the 2021 edition to support the ability to migrate to newer editions that introduce new keywords. This is analogous to [raw identifiers] which provide the same functionality for identifiers. For example, the 2024 edition introduced the `gen` keyword. Since lifetimes cannot be keywords, this would cause code that use a lifetime `'gen` to fail to compile. Raw lifetimes allow the migration lint to modify those lifetimes to `'r#gen` which do allow keywords.
10+
11+
In editions prior to 2021, raw lifetimes are parsed as separate tokens. For example `'r#foo` is parsed as three tokens: `'r`, `#`, and `foo`.
12+
13+
[raw identifiers]: ../../reference/identifiers.html#raw-identifiers
14+
15+
## Migration
16+
17+
As a part of the 2021 edition a migration lint, [`rust_2021_prefixes_incompatible_syntax`], has been added in order to aid in automatic migration of Rust 2018 codebases to Rust 2021.
18+
19+
In order to migrate your code to be Rust 2021 Edition compatible, run:
20+
21+
```sh
22+
cargo fix --edition
23+
```
24+
25+
Should you want or need to manually migrate your code, migration is fairly straight-forward.
26+
27+
Let's say you have a macro that is defined like so:
28+
29+
```rust
30+
macro_rules! my_macro {
31+
($a:tt $b:tt $c:tt) => {};
32+
}
33+
```
34+
35+
In Rust 2015 and 2018 it's legal for this macro to be called like so with no space between the tokens:
36+
37+
```rust,ignore
38+
my_macro!('r#foo);
39+
```
40+
41+
In the 2021 edition, this is now parsed as a single token. In order to call this macro, you must add a space before the identifier like so:
42+
43+
```rust,ignore
44+
my_macro!('r# foo);
45+
```
46+
47+
[`rust_2021_prefixes_incompatible_syntax`]: ../../rustc/lints/listing/allowed-by-default.html#rust-2021-prefixes-incompatible-syntax
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Cargo: Reject unused inherited default-features
2+
3+
## Summary
4+
5+
- `default-features = false` is no longer allowed in an inherited workspace dependency if the workspace dependency specifies `default-features = true` (or does not specify `default-features`).
6+
7+
## Details
8+
9+
[Workspace inheritance] allows you to specify dependencies in one place (the workspace), and then to refer to those workspace dependencies from within a package.
10+
There was an inadvertent interaction with how `default-features` is specified that is no longer allowed in the 2024 Edition.
11+
12+
Unless the workspace specifies `default-features = false`, it is no longer allowed to specify `default-features = false` in an inherited package dependency.
13+
For example, with a workspace that specifies:
14+
15+
```toml
16+
[workspace.dependencies]
17+
regex = "1.10.4"
18+
```
19+
20+
The following is now an error:
21+
22+
```toml
23+
[package]
24+
name = "foo"
25+
version = "1.0.0"
26+
edition = "2024"
27+
28+
[dependencies]
29+
regex = { workspace = true, default-features = false } # ERROR
30+
```
31+
32+
The reason for this change is to avoid confusion when specifying `default-features = false` when the default feature is already enabled, since it has no effect.
33+
34+
If you want the flexibility of deciding whether or not a dependency enables the default-features of a dependency, be sure to set `default-features = false` in the workspace definition.
35+
Just beware that if you build multiple workspace members at the same time, the features will be unified so that if one member sets `default-features = true` (which is the default if not explicitly set), the default-features will be enabled for all members using that dependency.
36+
37+
## Migration
38+
39+
When using `cargo fix --edition`, Cargo will automatically update your `Cargo.toml` file to remove `default-features = false` in this situation.
40+
41+
If you would prefer to update your `Cargo.toml` manually, check for any warnings when running a build and remove the corresponding entries.
42+
Previous editions should display something like:
43+
44+
```text
45+
warning: /home/project/Cargo.toml: `default-features` is ignored for regex,
46+
since `default-features` was not specified for `workspace.dependencies.regex`,
47+
this could become a hard error in the future
48+
```
49+
50+
[workspace inheritance]: ../../cargo/reference/specifying-dependencies.html#inheriting-a-dependency-from-a-workspace

src/rust-2024/cargo-resolver.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Cargo: Rust-version aware resolver
2+
3+
## Summary
4+
5+
- `edition = "2024"` implies `resolver = "3"` in `Cargo.toml` which enables a Rust-version aware dependency resolver.
6+
7+
## Details
8+
9+
Since Rust 1.84.0, Cargo has opt-in support for compatibility with
10+
[`package.rust-version`] to be considered when selecting dependency versions
11+
by setting [`resolver.incompatible-rust-version = "fallback"`] in `.cargo/config.toml`.
12+
13+
Starting in Rust 2024, this will be the default.
14+
That is, writing `edition = "2024"` in `Cargo.toml` will imply `resolver = "3"`
15+
which will imply [`resolver.incompatible-rust-version = "fallback"`].
16+
17+
The resolver is a global setting for a [workspace], and the setting is ignored in dependencies.
18+
The setting is only honored for the top-level package of the workspace.
19+
If you are using a [virtual workspace], you will still need to explicitly set the [`resolver` field]
20+
in the `[workspace]` definition if you want to opt-in to the new resolver.
21+
22+
For more details on how Rust-version aware dependency resolution works, see [the Cargo book](../../cargo/reference/resolver.html#rust-version).
23+
24+
[`package.rust-version`]: ../../cargo/reference/rust-version.html
25+
[`resolver.incompatible-rust-version = "fallback"`]: ../../cargo/reference/config.html#resolverincompatible-rust-versions
26+
[workspace]: ../../cargo/reference/workspaces.html
27+
[virtual workspace]: ../../cargo/reference/workspaces.html#virtual-workspace
28+
[`resolver` field]: ../../cargo/reference/resolver.html#resolver-versions
29+
30+
## Migration
31+
32+
There are no automated migration tools for updating for the new resolver.
33+
34+
We recommend projects
35+
[verify against the latest dependencies in CI](../../cargo/guide/continuous-integration.html#verifying-latest-dependencies)
36+
to catch bugs in dependencies as soon as possible.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Cargo: Table and key name consistency
2+
3+
## Summary
4+
5+
- Several table and key names in `Cargo.toml` have been removed where there were previously two ways to specify the same thing.
6+
- Removed `[project]`; use `[package]` instead.
7+
- Removed `default_features`; use `default-features` instead.
8+
- Removed `crate_type`; use `crate-type` instead.
9+
- Removed `proc_macro`; use `proc-macro` instead.
10+
- Removed `dev_dependencies`; use `dev-dependencies` instead.
11+
- Removed `build_dependencies`; use `build-dependencies` instead.
12+
13+
## Details
14+
15+
Several table and keys names are no longer allowed in the 2024 Edition.
16+
There were two ways to specify these tables or keys, and this helps ensure there is only one way to specify them.
17+
18+
Some were due to a change in decisions over time, and some were inadvertent implementation artifacts.
19+
In order to avoid confusion, and to enforce a single style for specifying these tables and keys, only one variant is now allowed.
20+
21+
For example:
22+
23+
```toml
24+
[dev_dependencies]
25+
rand = { version = "0.8.5", default_features = false }
26+
```
27+
28+
Should be changed to:
29+
30+
```toml
31+
[dev-dependencies]
32+
rand = { version = "0.8.5", default-features = false }
33+
```
34+
35+
Notice that the underscores were changed to dashes for `dev_dependencies` and `default_features`.
36+
37+
## Migration
38+
39+
When using `cargo fix --edition`, Cargo will automatically update your `Cargo.toml` file to use the preferred table and key names.
40+
41+
If you would prefer to update your `Cargo.toml` manually, be sure to go through the list above and make sure only the new forms are used.

src/rust-2024/cargo.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Cargo
2+
3+
The following chapters detail changes to Cargo in the 2024 Edition.

src/rust-2024/gen-keyword.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# `gen` keyword
2+
3+
## Summary
4+
5+
- `gen` is a [reserved keyword].
6+
7+
[reserved keyword]: ../../reference/keywords.html#reserved-keywords
8+
9+
## Details
10+
11+
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.
12+
13+
[RFC #3513]: https://rust-lang.github.io/rfcs/3513-gen-blocks.html
14+
15+
## Migration
16+
17+
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 overcome this, Rust supports the `r#` prefix for a [raw identifier], which allows identifiers to overlap with keywords.
18+
19+
The [`keyword_idents_2024`] lint will automatically modify any identifier named `gen` to be `r#gen` so that 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:
20+
21+
```sh
22+
cargo fix --edition
23+
```
24+
25+
For example, this will change:
26+
27+
```rust
28+
fn gen() {
29+
println!("generating!");
30+
}
31+
32+
fn main() {
33+
gen();
34+
}
35+
```
36+
37+
to be:
38+
39+
```rust
40+
fn r#gen() {
41+
println!("generating!");
42+
}
43+
44+
fn main() {
45+
r#gen();
46+
}
47+
```
48+
49+
Alternatively, you can manually enable the lint to find places where `gen` identifiers need to be modified to `r#gen`:
50+
51+
```rust
52+
// Add this to the root of your crate to do a manual migration.
53+
#![warn(keyword_idents_2024)]
54+
```
55+
56+
[raw identifier]: ../../reference/identifiers.html#raw-identifiers
57+
[`keyword_idents_2024`]: ../../rustc/lints/listing/allowed-by-default.html#keyword-idents-2024

src/rust-2024/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Rust 2024
2+
3+
| Info | |
4+
| --- | --- |
5+
| RFC | [#3501](https://rust-lang.github.io/rfcs/3501-edition-2024.html) |
6+
| Release version | 1.85.0 |

0 commit comments

Comments
 (0)