Skip to content

Commit ca73087

Browse files
committed
Basic dyn keyword without caveats
1 parent 952622e commit ca73087

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

src/types.md

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ Because captures are often by reference, the following general rules arise:
508508

509509
> **<sup>Syntax</sup>**
510510
> _TraitObjectType_ :
511-
> &nbsp;&nbsp; _LifetimeOrPath_ ( `+` _LifetimeOrPath_ )<sup>\*</sup> `+`<sup>?</sup>
511+
> &nbsp;&nbsp; `dyn`<sup>?</sup> _LifetimeOrPath_ ( `+` _LifetimeOrPath_ )<sup>\*</sup> `+`<sup>?</sup>
512512
>
513513
> _LifetimeOrPath_ :
514514
> &nbsp;&nbsp; [_Path_] | [_LIFETIME_OR_LABEL_]
@@ -520,31 +520,43 @@ number of [auto traits].
520520
Trait objects implement the base trait, its auto traits, and any super traits
521521
of the base trait.
522522

523-
Trait objects are written the same as trait bounds, but with the following
524-
restrictions. All traits except the first trait must be auto traits, there may
525-
not be more than one lifetime, and opt-out bounds (e.g. `?sized`) are not
526-
allowed. For example, given a trait `Trait`, the following are all trait
527-
objects: `Trait`, `Trait + Send`, `Trait + Send + Sync`, `Trait + 'static`,
528-
`Trait + Send + 'static`, `Trait +`, `'static + Trait`.
523+
Trait objects are written as the optional keyword `dyn` followed by a set of
524+
trait bounds, but with the following restrictions on the trait bounds. All
525+
traits except the first trait must be auto traits, there may not be more than
526+
one lifetime, and opt-out bounds (e.g. `?sized`) are not allowed. For example,
527+
given a trait `Trait`, the following are all trait objects:
528+
529+
* `Trait`
530+
* `dyn Trait`
531+
* `dyn Trait + Send`
532+
* `dyn Trait + Send + Sync`
533+
* `dyn Trait + 'static`
534+
* `dyn Trait + Send + 'static`
535+
* `dyn Trait +`
536+
* `dyn 'static + Trait`.
537+
538+
> Note: For clarity, it is recommended to always use the `dyn` keyword on your
539+
> trait objects unless your codebase supports compiling with Rust 1.26 or lower.
529540
530541
Two trait object types alias each other if the base traits alias each other and
531542
if the sets of auto traits are the same and the lifetime bounds are the same.
532-
For example, `Trait + Send + UnwindSafe` is the same as
533-
`Trait + Unwindsafe + Send`.
543+
For example, `dyn Trait + Send + UnwindSafe` is the same as
544+
`dyn Trait + Unwindsafe + Send`.
534545

535546
> Warning: With two trait object types, even when the complete set of traits is
536547
> the same, if the base traits differ, the type is different. For example,
537-
> `Send + Sync` is a different type from `Sync + Send`. See [issue 33140].
548+
> `dyn Send + Sync` is a different type from `dyn Sync + Send`. See
549+
> [issue 33140].
538550
539551
> Warning: Including the same auto trait multiple times is allowed, and each
540-
> instance is considered a unique type. As such, `Trait + Send` is a distinct
541-
> type than `Trait + Send + Send`. See [issue 47010].
552+
> instance is considered a unique type. As such, `dyn Trait + Send` is a
553+
> distinct type to `dyn Trait + Send + Send`. See [issue 47010].
542554
543555
Due to the opaqueness of which concrete type the value is of, trait objects are
544556
[dynamically sized types]. Like all
545557
<abbr title="dynamically sized types">DSTs</abbr>, trait objects are used
546-
behind some type of pointer; for example `&SomeTrait` or `Box<SomeTrait>`. Each
547-
instance of a pointer to a trait object includes:
558+
behind some type of pointer; for example `&dyn SomeTrait` or
559+
`Box<dyn SomeTrait>`. Each instance of a pointer to a trait object includes:
548560

549561
- a pointer to an instance of a type `T` that implements `SomeTrait`
550562
- a _virtual method table_, often just called a _vtable_, which contains, for
@@ -568,12 +580,12 @@ impl Printable for i32 {
568580
fn stringify(&self) -> String { self.to_string() }
569581
}
570582

571-
fn print(a: Box<Printable>) {
583+
fn print(a: Box<dyn Printable>) {
572584
println!("{}", a.stringify());
573585
}
574586

575587
fn main() {
576-
print(Box::new(10) as Box<Printable>);
588+
print(Box::new(10) as Box<dyn Printable>);
577589
}
578590
```
579591

0 commit comments

Comments
 (0)