3

I am on chapter 19 of The Rust Programming Language when I saw this code

Box<dyn Fn() + Send + 'static>

What does it do and why is there the plus sign (+) in there? I don't remember encountering this in a previous chapter.

How is Box::new(|| println!("hi")) a Box<dyn Fn() + Send + 'static>?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
zer09
  • 1,507
  • 2
  • 28
  • 48
  • 3
    take a look at the [rust by example](https://doc.rust-lang.org/rust-by-example/generics/bounds.html) you also should reread the chapiter 10 of the rust-lang book, the `+` operator is used to combie bounds from cargo doc: ```When working with generics, the type parameters often must use traits as bounds to stipulate what functionality a type implements. For example, the following example uses the trait Display to print and so it requires T to be bound by Display; that is, T must implement Display.``` in your case the `Box` contains a dynamic object that implement `Send` with `'static` lifetime – Asya Corbeau Oct 08 '19 at 00:19

1 Answers1

4

Thanks to @Asya Corbeau pointed me back to chapter 10, it is the Specifying Multiple Trait Bounds with the + Syntax

zer09
  • 1,507
  • 2
  • 28
  • 48