How to: fmt.Println 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.
GOlang
In Golang you can output text and variables to the terminal via various methods, but fmt is a commonly used one.
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
// Hello Jonathan!
name := "Jonathan"
fmt.Printf("Hello %s!\n", name)
fmt.Printf("1 + 1 is %d!\n", 1+1)
// %v for unknown variable types
fmt.Printf("unknown variable type: %v without newline", 40.1)
}
More fmt.Println 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:#?}")
}