CARDIAC › rust-cardiac › Message codes › RCE0039
RCE0039error
A trait object used this way.
error[RCE0039]: this use of a trait object isn't supported: a `&dyn Trait` works where it is made of a reference to one of the program's structs, and a method called through it is one each struct's impl defines, and a `&dyn Fn(..)` where it is made of a closure that captures ({why})
A trait object (&dyn Trait, Box<dyn Trait>) used in a way the compiler can't follow.
A &dyn Trait compiles as an enum of the program's types that are made into one, and a method call through it as a match on which type it is. That needs every such object to be a reference to one of the program's own structs, and each method one their impls define. A &dyn Fn(..) is the same with closures: each one made into it must capture something (a closure that captures nothing can be a function pointer, fn(i16) -> i16). A Box<dyn Trait>, a &mut dyn FnMut(..), or a trait object of a library type, isn't.
Use a &dyn Trait of your own structs, or an enum and a match.
enum Shape { Square(i16), Line(i16) }
fn area(s: &Shape) -> i16 {
match s {
Shape::Square(a) => a * a,
Shape::Line(_) => 0,
}
}
In a terminal: cargo cardiac help RCE0039.