What is the difference between use std::io::prelude::*; and use std::io::*; in the following snippet

Hi Rust experts,

What is the difference between use std::io::prelude::; and use std::io::; in the following snippet

use std::fs::File;
use std::io::prelude::*;

fn main () {
    let mut f = File::open("hello.txt").unwrap();
    let mut contents = String::new();
    f.read_to_string(&mut contents).unwrap();
    println!("{}", contents);
}

std::io::prelude exports only those 4 traits :

pub use super::BufRead;	
pub use super::Read;	
pub use super::Seek;	
pub use super::Write;

std::io exposes all of those structs, enums, traits and functions.

1 Like

So basically it makes the imports cleaner (making it easier to tell what's imported from where), but does it have other effects: Compile time? Execution time? Binaries's sizes.

  • No the standard library is packaged already quasi-compiled (so that you don't have to recompile all of it).
  • Depending on the functions you use from there and their respective level of optimization, no. It's hard to understand what you mean here.
  • And no, the standard library is mostly always compiled in your executable no matter what. (except for #![no_std] environments)
1 Like

What you import has no effect on compile or run time in any reasonable situation.

4 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.