Skip to content

Instantly share code, notes, and snippets.

@rpivo
Last active July 7, 2021 23:18
Show Gist options
  • Save rpivo/a5d40a0746481af45036ff26579f65a8 to your computer and use it in GitHub Desktop.
Save rpivo/a5d40a0746481af45036ff26579f65a8 to your computer and use it in GitHub Desktop.
Bringing Types Into Scope With the `Use` Keyword in Rust

Bringing Types Into Scope With the Use Keyword in Rust

Using ES modules in JavaScript, you would import something into a module like so:

import thing from "stuff";

We can achieve similar behavior with Rust types using the use keyword. In the example below, we're importing io from std.

use std::io;

fn main() {
    let mut guess = String::new();

    io::stdin()
        .read_line(&mut guess)
        .expect("Failed to read line");

    println!("{}", guess);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment