Hi, if you're ok with limiting Statement to 'static types, you can add a function type_id (for example) to it to get the TypeId. It would be 100% safe and a lot less complicated.
This will help in getting the underlying type but I want to get the data in the concrete type as well. So, I can compare the actual output with the expected out.
From waht you are describing I guess you try to built an Abstract Syntax Tree. In this case you have usually a limited set of Statements, therefore, I would recommend to build an "enum of new types". Genrally, you build your own structs that implement the Statement trait like the Let you mentioned. Then you create an enum that covers all of your possible Statements, e.g.:
struct Let { id: Identifier }
struct Identifier(String);
imple Statement for Let {}
enum Ast {
Let(Let),
// ... other statements
}
impl Ast {
fn is_let_with_id(&self, id: &str) -> bool {
if let Let(l) = self {
&l.id == id
} else {
false
}
}
}
With this setup you can match against the Ast enum to get the underlying type Let without having to have to work with trait-objects.