@@ -508,7 +508,7 @@ Because captures are often by reference, the following general rules arise:
508
508
509
509
> ** <sup >Syntax</sup >**
510
510
> _ TraitObjectType_ :
511
- >   ;  ; _ LifetimeOrPath_ ( ` + ` _ LifetimeOrPath_ )<sup >\* </sup > ` + ` <sup >?</sup >
511
+ >   ;  ; ` dyn ` < sup >?</ sup > _ LifetimeOrPath_ ( ` + ` _ LifetimeOrPath_ )<sup >\* </sup > ` + ` <sup >?</sup >
512
512
>
513
513
> _ LifetimeOrPath_ :
514
514
>   ;  ; [ _ Path_ ] | [ _ LIFETIME_OR_LABEL_ ]
@@ -520,31 +520,43 @@ number of [auto traits].
520
520
Trait objects implement the base trait, its auto traits, and any super traits
521
521
of the base trait.
522
522
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.
529
540
530
541
Two trait object types alias each other if the base traits alias each other and
531
542
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` .
534
545
535
546
> Warning: With two trait object types, even when the complete set of traits is
536
547
> 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] .
538
550
539
551
> 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] .
542
554
543
555
Due to the opaqueness of which concrete type the value is of, trait objects are
544
556
[ dynamically sized types] . Like all
545
557
<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:
548
560
549
561
- a pointer to an instance of a type ` T ` that implements ` SomeTrait `
550
562
- a _ virtual method table_ , often just called a _ vtable_ , which contains, for
@@ -568,12 +580,12 @@ impl Printable for i32 {
568
580
fn stringify (& self ) -> String { self . to_string () }
569
581
}
570
582
571
- fn print (a : Box <Printable >) {
583
+ fn print (a : Box <dyn Printable >) {
572
584
println! (" {}" , a . stringify ());
573
585
}
574
586
575
587
fn main () {
576
- print (Box :: new (10 ) as Box <Printable >);
588
+ print (Box :: new (10 ) as Box <dyn Printable >);
577
589
}
578
590
```
579
591
0 commit comments