Questions tagged [trait-objects]

For questions related to Trait Objects (dynamically-dispatched objects) in Rust

Trait objects are the dynamically dispatched objects in Rust. They are unsized, so they are used behind some kind of reference (Box, for example).

Links:

183 questions
0
votes
0 answers

How to convert trait object to "sub-trait" object?

This code: trait A {} trait B: A {} struct S; impl A for S {} impl B for S {} fn main() { let s = S; let trait_obj_b: &B = &s; let trait_obj_a: &A = trait_obj_b; } fails with the error: error[E0308]: mismatched types -->…
0
votes
1 answer

How are trait objects implemented in Rust?

I'm trying to understand how trait objects are implemented in Rust. Please let me know if the following understanding is correct. I have a function that takes any type that implements the Write trait: fn some_func(write_to: &mut Write) {} In any…
user3169543
  • 1,499
  • 1
  • 10
  • 16
-1
votes
0 answers

rust trait object dispatch compiler error

I'm struggling in dynamic dispatch for the whole day. try to work trait object with generic parameter, I know that can't be made into object. But I still want to write Object-Oriented code. For writing oo code, there are many Box types in…
holi-java
  • 29,655
  • 7
  • 72
  • 83
1 2 3
12
13