How to: console.log in Rust

Outputting to your shell or terminal to inform the user of the outcome of a program or valuable debugging information is an essential of every programming language.

JavaScript

In JavaScript console.log will print to the browser console or your shell where node.js is running your file:

// strings
console.log('Hello World!');
// multiple parameters
console.log('Hello', 'World!', 666);

More console.log References

Rust

In rust we can use the println! macro to write to output strings and variables.

fn main() {
  println!("Hello World!");
  println!("My name is {}", "Jonathan");
  println!("NOT true is {}", !true);
}

If we want to log the shape of a struct with its values we need to use a different placeholder in our log statement:

#![allow(unused)]
fn main() {
  #[derive(Debug)]
  struct Point {
    x: i32,
    y: i32,
  }

  let origin = Point { x: 5, y: 13 };

  println!("point debug: {origin:#?}")
}

More println References