# Trait bound vs. dyn keyword

**URL:** <https://users.rust-lang.org/t/trait-bound-vs-dyn-keyword/53929>\
**Category:** uncategorized\
**Created:** [January 9, 2021, 8:06pm UTC](https://users.rust-lang.org/t/trait-bound-vs-dyn-keyword/53929 "2021-01-09T20:06:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvolkmann](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mvolkmann/32/18387_2.png) [@mvolkmann](https://users.rust-lang.org/u/mvolkmann)\
**Post date:** [January 9, 2021, 8:06pm UTC](https://users.rust-lang.org/t/trait-bound-vs-dyn-keyword/53929/1 "2021-01-09T20:06:57Z")

</div>

Is there any difference between these two approaches? The both seem to work fine.

```rust
use core::fmt::Debug;

#[derive(Debug)]
struct Point2D {
    x: f64,
    y: f64
}

impl ToString for Point2D {
    fn to_string(&self) -> String {
        format!("({}, {})", self.x, self.y)
    }
}

// Approach #1: fn print_string(label: &str, value: &dyn ToString) {
// Approach #2: fn print_string<T: ToString>(label: &str, value: &T) {
fn print_string(label: &str, value: &dyn ToString) { // using approach #1 here
  println!("{}: {}", label, value.to_string());
}

fn main() {
  print_string("Name", &"Mark"); // Name: Mark
  print_string("Score", &19); // Score: 19
  let pt = Point2D { x: 1.2, y: 3.4 };
  print_string("Point", &pt); // Point: (1.2, 3.4)
}

```

([Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d431d70684ff742f264fbc557587725c))

Output:

```rust
Name: Mark
Score: 19
Point: (1.2, 3.4)

```

Errors:

```rust
   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 0.89s
     Running `target/debug/playground`

```

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [January 9, 2021, 8:15pm UTC](https://users.rust-lang.org/t/trait-bound-vs-dyn-keyword/53929/2 "2021-01-09T20:15:34Z")

</div>

Yes, when using `dyn ToString`, the executable will contain only one version of `print_string`, but when using `<T: ToString>`, the executable will contain a separate copy of `print_string` for each type `T` you use it with.

Note that using `impl ToString` is equivalent to `<T: ToString>`, and _not_ to `dyn ToString`.

---

<div class="post-metadata">

**Author:** ![mvolkmann](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mvolkmann/32/18387_2.png) [@mvolkmann](https://users.rust-lang.org/u/mvolkmann)\
**Post date:** [January 9, 2021, 8:34pm UTC](https://users.rust-lang.org/t/trait-bound-vs-dyn-keyword/53929/3 "2021-01-09T20:34:04Z")

</div>

If I want the parameter to implement more than one trait, I can do this which works:

```rust
fn print_string<T: Debug + ToString>(label: &str, value: &T) {

```

How can I specify multiple traits with the `dyn` keyword?  
I tried this, but the compiler doesn't like it:

```rust
fn print_string(label: &str, value: &(dyn Debug + ToString)) {

```

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [January 9, 2021, 8:36pm UTC](https://users.rust-lang.org/t/trait-bound-vs-dyn-keyword/53929/4 "2021-01-09T20:36:41Z")

</div>

Trait objects only allow one "real" trait. To use multiple traits, define a helper trait

```rust
use std::fmt::Debug;

fn print_string(label: &str, value: &(dyn DebugAndToString)) {
}

trait DebugAndToString: Debug + ToString {}
impl<T: Debug + ToString> DebugAndToString for T {}

```

---

<div class="post-metadata">

**Author:** ![mvolkmann](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mvolkmann/32/18387_2.png) [@mvolkmann](https://users.rust-lang.org/u/mvolkmann)\
**Post date:** [January 9, 2021, 10:33pm UTC](https://users.rust-lang.org/t/trait-bound-vs-dyn-keyword/53929/5 "2021-01-09T22:33:31Z")

</div>

Thanks so much Alice! You've been SO helpful to me!

---

<div class="post-metadata">

**Author:** ![Cerber-Ursi](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cerber-ursi/32/8315_2.png) [@Cerber-Ursi](https://users.rust-lang.org/u/Cerber-Ursi)\
**Post date:** [January 10, 2021, 8:39am UTC](https://users.rust-lang.org/t/trait-bound-vs-dyn-keyword/53929/6 "2021-01-10T08:39:50Z")

</div>

> [@alice](#):
>
> the executable will contain a separate copy of `print_string` for each type `T` you use it with.

...and will be able to optimize them separately, knowing the exact implementation of trait, which is sometimes important.

---

<div class="post-metadata">

**Author:** ![Yandros](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/yandros/32/8136_2.png) [@Yandros](https://users.rust-lang.org/u/Yandros)\
**Post date:** [January 10, 2021, 1:16pm UTC](https://users.rust-lang.org/t/trait-bound-vs-dyn-keyword/53929/7 "2021-01-10T13:16:41Z")

</div>

> [@What's the difference between T: Trait and dyn Trait?](https://users.rust-lang.org/t/whats-the-difference-between-t-trait-and-dyn-trait/35381/3):
>
> \<T : Trait\> means that the function / trait / etc. can be "fed" / "instanced with", at compile time, any type T as long as T implements Trait. For each different T, a different version of the code will be generated / copy-pasted. dyn Trait, on the other hand, represents only one single concrete type, the one that unifies "all" the (other) T : Trait. Indeed, any (slim) pointer to a T when T : Trait can be coerced into a (fat) pointer to the type dyn Trait. And the type dyn Trait has an autom…

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [April 10, 2021, 1:16pm UTC](https://users.rust-lang.org/t/trait-bound-vs-dyn-keyword/53929/8 "2021-04-10T13:16:48Z")

</div>

Closed on April 10, 2021, 1:16pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
