Downcasting a value stored in HashMap and returning Option<&T>

⚓ Rust    📅 2025-05-03    👤 surdeus    👁️ 4      

surdeus

Warning

This post was published 109 days ago. The information described in this article may have changed.

Hi all, I would really appreciate it if someone could take a look at the code below and tell me why the code doesn't work? Also what can I do to get this to work.

Short summary of what I am trying to achieve:

  1. I have a trait for example SomeTrait
  2. I have a struct Bar which contains a field data which is HashMap<String, Box>

I am writing a method get<T> for Bar which should access the stored value and return Option<&T>

Following is the example code I have:

EDIT: Fixed the link

So my question is if there is an ideal way to fix this? Preferably by not creating a new owned value?

7 posts - 5 participants

Read full topic

🏷️ rust_feed