Proper format for error code explanations

This commit is contained in:
Ryan Levick 2021-03-18 13:22:25 +01:00
parent 1d84947bb5
commit 152c86211b
2 changed files with 31 additions and 14 deletions

View File

@ -1,17 +1,26 @@
Trait objects must include the `dyn` keyword.
Trait objects are a way to call methods on types that are not known until
runtime but conform to some trait.
Erroneous code example:
In the following code the trait object should be formed with
`Box<dyn Foo>`, but `dyn` is left off.
```no_run
```edition2021,compile_fail,E782
trait Foo {}
fn test(arg: Box<Foo>) {}
```
Trait objects are a way to call methods on types that are not known until
runtime but conform to some trait.
Trait objects should be formed with `Box<dyn Foo>`, but in the code above
`dyn` is left off.
This makes it harder to see that `arg` is a trait object and not a
simply a heap allocated type called `Foo`.
To fix this issue, add `dyn` before the trait name.
```
trait Foo {}
fn test(arg: Box<dyn Foo>) {}
```
This used to be allowed before edition 2021, but is now an error.

View File

@ -1,18 +1,26 @@
The range pattern `...` is no longer allowed.
Older Rust code using previous editions allowed `...` to stand for exclusive
ranges which are now signified using `..=`.
Erroneous code example:
The following code use to compile, but now it now longer does.
```no_run
```edition2021,compile_fail,E782
fn main() {
let n = 2u8;
match n {
...9 => println!("Got a number less than 10"),
match 2u8 {
0...9 => println!("Got a number less than 10"),
_ => println!("Got a number 10 or more")
}
}
```
Older Rust code using previous editions allowed `...` to stand for exclusive
ranges which are now signified using `..=`.
To make this code compile replace the `...` with `..=`.
```
fn main() {
match 2u8 {
0..=9 => println!("Got a number less than 10"),
_ => println!("Got a number 10 or more")
}
}
```