Importing modules in module,

I'm quite new to Rust. I wanted to print someones name after they input it which I successfully did by using:

use std::io;

fn main() {
    let mut name = String::new();
    io::stdin().read_line(&mut name).expect("Failed to read line");
    name = name.trim().to_string();
    print!("Hello {}!", name)

But instead of doing that everytime I wanted to get input, I thought it would be a better idea to create a module for it (for general usage, so not just for input)

So I made a file called "" (couldn't think of a good name) which contains:

use std::io;

pub fn input() -> std::string::String {
    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("Failed to read line");
    input = input.trim().to_string();

But apparently I can't do it this way, it returns the error: error[E0658]: access to extern crates through prelude is experimental (see issue #44660)

How would I be able to do this so that I can do the following from

mod custom;

fn main() {

    println!("What is your name?");
    //let mut name = String::new();
    let mut name = custom::input();

    print!("Hello {}!", name);


I guess it's this line that rustc warns about.

This will be valid code soon (in Rust 2018), but for now, you need to prefix absolute paths with ::, so:

pub fn input() -> ::std::string::String

Note that you didn't have to type ::std in your use std::io statement – use statements behaving differently are exactly something that the new edition wants to make more predictable.

Anyway, you could just write

pub fn input() -> String

as String is always visible by default (it's in the prelude).

1 Like

Thank you, that fixed it! :grinning:

Things to keep in mind about module system:

  • paths in use are absolute.
  • paths in code are relative. Both appear to work the same in, but the difference will become apparent in submodules.
  • mod defines a module in the place where it is, the same way fn or struct create new names. That's very different from require/import in other languages.
1 Like