Calling methods on a BufRead trait object

⚓ Rust    📅 2025-07-10    👤 surdeus    👁️ 5      

surdeus

I'm working through the Command Line Rust book and just completed the headr program. The provided open function returns a file handle as a Box<dyn BufRead> type. In his solution, the author calls a read method on this object, which isn't present on BufRead's interface. My understanding was that, because you only know that you're dealing with something that implements BufRead, you would only be able to call those methods BufRead itself provides.

I see in the docs that the Read trait has the read method, and is listed as implementing BufRead, but that doesn't seem like it's the correct relationship for BufRead to also have read available.

What am I misunderstanding? Am I reading the docs incorrectly?

3 posts - 2 participants

Read full topic

🏷️ rust_feed