2

I'm learning Rust by looking at and manipulating examples from others. I tried to encapsulate the following code into a struct:

let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();

I want to create a struct like this:

pub struct Window {
    encoder: gfx::Encoder<?, ?>,
    // ...
}

How do I know what types I have to put into the question marks?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Timm
  • 1,005
  • 8
  • 20

1 Answers1

3

Change the type of your variable (encoder) to cause a type mismatch. The easiest type to use for this is ():

let mut encoder: gfx::Encoder<(), ()> = factory.create_command_buffer().into();

This will generate an error with the concrete types, which you can then clean up and use directly.

See also How do I print the type of a variable in Rust?.


In many cases, you'd use something a bit simpler:

let mut encoder: () = factory.create_command_buffer().into();

But that's unlikely to work in this specific case because into has a polymorphic return type. It needs to have some concrete type specified in order to know which implementation should be called.

Community
  • 1
  • 1
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366