Skip to content

Instantly share code, notes, and snippets.

@rpivo
Last active July 8, 2021 20:45
Show Gist options
  • Save rpivo/3044893b17fa9de62c453e02c4d96377 to your computer and use it in GitHub Desktop.
Save rpivo/3044893b17fa9de62c453e02c4d96377 to your computer and use it in GitHub Desktop.
Using `std::println` in Rust

Using std::println in Rust

Rust's std::println is similar to loggers in other languages.

fn main() {
    println!("Hello, world!"); // Hello, world!
}

To pass in arguments to println, use {}.

fn main() {
    println!("Hello, {}!", "world"); // Hello, world!
}
fn main() {
    println!("{}, {}!", "Hello", "world"); // Hello, world!
}

Note that array data types like vectors will not directly work with println. See this Stack Overflow answer for details.

However, you can use a for loop to do something like this:

fn main() {
    let mut vec = Vec::new();
    vec.push(1);
    vec.push(2);
    vec.push(3);
    for x in &vec {
        println!("{}", x);
    }
}

// 1
// 2
// 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment