Trait objects without an explicit 'dyn' are deprecated

I just looked at some code I wrote almost two years ago, and my IDE gave me this warning for fn bar().

pub trait Foo: Sized {
    fn bar(&self, s: &str) -> Self;
    fn qux(&self, s: &str) -> Self { self.bar(s) }
}

If I change that to

pub trait Foo: Sized {
    fn bar(&self, s: &str) -> dyn Self;
    fn qux(&self, s: &str) -> Self { self.bar(s) }
}

I get "The trait bound Foo: std::marker::Sized is not satisfied" on fn bar(). Replacing Self with Foo doesn't help. If I change Foo: Sized to Foo, I get an error on fn qux() saying that Self isn't sized.

I can leave things as I found them, but I don't want to be accused of being old fashioned

1 Like

Clippy doesn't have any problems with your first code block, and your second one is not valid syntax. dyn can only be used on traits to signify that they are being used as trait objects.

So, it's a false positive from the IDE. I'll try to remember to run clippy before worrying about such things.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.