IO and prelude - error[E0433]: failed to resolve: use of undeclared crate or module `io`

I am not clear about "IO and prelude" - I get error[E0433]: failed to resolve: use of undeclared crate or module io.

See video: https://youtu.be/IKUbjL73nC0

I must be incorrectly assuming that “prelude” includes everything in “std::io”
or
I must learn more about keyword “use” ...

use std::fs::File;
use std::io; // **remove this to trigger error**
use std::io::prelude::*;

fn main() -> io::Result<()> {
    let mut f = File::open("foo.txt")?;
    let mut buffer = Vec::new();

    f.read_to_end(&mut buffer)?;
    Ok(())
}

error[E0433]: failed to resolve: use of undeclared crate or module io

Similar questions:

This requires that you have io somewhere in your type namespace. Without use std::io; in your example, there is no such name available, hence the error message.

std::io::prelude does not contain everything inside of the std::io module. You can see what it reëxports here.

Prelude modules are normal modules that are used by library developers to export items in bulk (mostly traits) to decrease the verbosity of necessary imports of things people would consider utilitarian in nature—implementation details. They are a tool for the convenience of library consumers, so to speak, and not to be confused with the prelude provided by the Rust edition you are using. My favorite example of a sensible prelude module is rayon::prelude. I don't like polars::prelude, which contains too many types IMO.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.