rustips How to print your Structs the way you want it

How to print your Structs the way you want it

The Struct Let’s keep it simple here with a struct for a 2D Point in space: pub struct Point { x: i32, y: i32 }   1st (fail) try If you try to print an instance of your struct you will get a panicked compiler let p = Point{x: 10, y: 5}; println!(“{}”, p); error[E0277]: …

Read Article Read More

rustips Convert user input to Numeral

Convert user input to Numeral

Input to Numeral This method is useful when the expected input will contain a simple number. If you have to parse the sentence for the number, this is not the method you should be using. let mut num1 = String::new(); println!(“What is the first number? “); stdin().read_line(&mut num1).expect(“Invalid input”); let num1: i32 = num1.trim().parse::<i32>().expect(“We’re expecting a number”); …

Read Article Read More

rustips Get user input as text

Get user input as text

Input as text use std::io::stdin println!(“What is your name?”); let mut name = String::new(); stdin().read_line(&mut name).expect(“Couldn’t read the line”); println!(“Hi {}!”, name); What is your name? John Hi John! We needed: to import the standard input module (stdin) create a mutable variable to serve as the buffer for the reader and hold the user’s answer …

Read Article Read More

rustips Print in same line

Print in same line

“Normal” Print If you want to print with a line break at the end, it is pretty easy: println!(“Here is your message!”); println!(“With {} placeholders too!”, 1); Which would output: Here is your message! With 1 placeholders too!   Print in the same line This needs some more increments. We need to import some things …

Read Article Read More