Method cannot be invoked on a trait object until its supertrait is brought into scoope

⚓ Rust    📅 2025-07-11    👤 surdeus    👁️ 3      

surdeus

I was trying to call bytes on a BufRead trait object and initially received the error that the method couldn't be invoked because bytes has Sized requirement. However, if std::io::Read is brought into scope, the call to bytes goes through. I understand that bytes is a method of the Read trait, so having the use statement for it makes sense, but I'm having a bit of trouble understanding how adding the import seems to suddenly make a trait object Sized.

I did a bit of research and my intuition is that perhaps the compiler doesn't create the concrete method to dispatch to without the import of Read, but I don't have the knowledge to say for certain if that's the case or not. Could anyone give me some more insight as to what's happening here? Thanks!

2 posts - 2 participants

Read full topic

🏷️ rust_feed