Why do I need to 'use std::io::prelude::*' to use BufReader.read_line?

Why do I need use std::io::prelude::* to use BufReader.read_line? I'm looking at the example in the BufReader documentation.

Also, I thought the prelude is automatically included?

The only prelude that is automatically included is the main std prelude, nothing else. std::io::prelude is a normal module with nothing special going on, but it follows a common pattern in larger modules/crates to put the most common parts in a module named prelude for ease of use. You could use std::io::{BufRead, BufReader}; manually to use BufReader::read_line, but importing everything from prelude is simpler.

1 Like

Cool thanks

Just to clarify for myself, BufReader implements the trait BufRead and the read_line method is defined in the BufRead trait. So if we want to use the method from the trait we have to import the trait.


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