Rust in Motion

Introduction to Rust

notion image
Ownership and Borrowing

Error Handling



Resume from here:

Guessing game

use std::io; // prelude is used to accept user input fn main() { println!("Guess the number!"); // any function name which ends with ! is called macro which ideally is not a funtion. println!("Please input your guess."); let mut guess = String::new(); // created a mutable variable that is currently bound to a new, empty instance of a String. io::stdin() .read_line(&mut guess) .expect("Failed to read line"); println!("You guessed: {guess}"); }

Associated Function

The :: syntax in the ::new line indicates that new is an associated function of the String type.
An associated function is a function that’s implemented on a type, in this case String.
This new function creates a new, empty string. You’ll find a new function on many types, because it’s a common name for a function that makes a new value of some kind.
`std` is standard library. `io` is the module. `stdin` is the function. `read_line` is the method.
Rust is awesome, but I have to learn Python frameworks first to build things. I can move to Rust or Go in a later point of time when Python becomes a bottleneck.