1

I have a Box I would like to downcast to specific struct. I know it's possible for Box, but since I don't use Any, I can't do that. Is there a way to downcast dyn?

krojew
  • 1,297
  • 15
  • 38
  • Then please accept the suggested duplicate :) – hellow Aug 06 '19 at 09:28
  • 1
    What would be an actual use case of this that is justifiable and impossible to do in any other way? Normally traits are used exactly when you *don't care* about the type of whatever struct that still requires *functionality* that you expect. I'd suggest using enums and have each type stored in it's own variant. More/less typing isn't really an argument as you type more by checking for types and trying to downcast... –  Aug 06 '19 at 09:28
  • 1
    @Sahsahae one use case is using mocks in tests, where I'm sure what object I'm expecting, but I have to deal with boxes and dyn traits. – krojew Aug 06 '19 at 09:30
  • I think you should provide a minimal example in your question, that reflects your actual problem, it will be easier for people to answer for that specific issue. –  Aug 06 '19 at 09:32

0 Answers0