Does Rust have RTTI?

I frequently read on topics saying Rust has no RTTI. But Rust does have the Any trait which has type id and downcast. That probably means Any is not RTTI? Would someone please point out which part of RTTI is missing in Rust? I don't have a precise definition of RTTI either, so that may be explained a bit in the answer too.

You can't find the names of random member functions (and then make calls to said functions,) by just sending the structure to some RTTI handling function.

Rust has type erasure; the compiler doesn't add extra metadata to allow applications to be able query whether a cast is valid or not in runtime. With that said, it does have mechanisms that allows you to opt in to approximate/simulate such mechanisms where you may need it (as you've noticed in Any).

You can sort of look at it as the Rust Elders not wanting there to be a bunch of implicit metadata created, but opening the door to allowing it (sort of) in specific cases.

I bet nobody has a precise (let alone useful) definition of what "RTTI" is. It's a silly thing to argue about arbitrary definitions of natural-language expressions, so I usually recommend people to not try and equate Rust concepts exactly with concepts from other languages. The important thing is what you can or cannot do, and how you can do it.

Rust's Any can perform downcasting. That's what most people need RTTI for in C++; I'm actually not sure what other specific, legitimate uses there may be. I in fact have never needed it for anything else during my 10+ years of writing C++ in professional and hobbyist settings, so I'm perfectly satisfied with the capabilities of Any in Rust.

the catch clause need to decide whether the exception being thrown is a subtype of the exception type it wanted to catch (assuming it's catching the exception by reference or pointer). also, search std::type_info on github, a lot of result will show up, so I guess it's not complete useless.

But how is that an "other" usecase? It's precisely downcasting.

ha, you are right. when I saw the word downcasting, my brain is thinking the dynamic_cast operator.

Given the question, a main thing to keep in mind for Any and TypeId are perhaps their restrictions.

  • Your type must satisfy the 'static bound to have a TypeId at all
  • You can't coerce unsized types to dyn Any, even if they have a TypeId

If you actually want to utilize downcasting with anything besides dyn Any, such as your own erased dyn MyTrait types, it involves a bit of a dance. It will become slightly simpler when we get supertrait upcasting.

It's a somewhat niche ability in part because of the restrictions (you generally don't want a 'static requirement on your trait if you don't need one). But also because it's not very idiomatic to be attempting to downcast all over the place. Often this means you should have used an enum or something else instead.

5 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.