Table of contents
So in the last article we have started learning about the basic syntax of rust where we learnt about function or (fn) main and variables and printing lines . Those who didn't check the last article view it to catch up .
Now we are going to learn how to get input in rust . For input and output in rust we have to use the standard library . So in the standard library there is a crate named io which stands for input/output .
In Detail about the standard library
How to use input / output ?
Input/Output crate is used to for input or to get a output from the user
use std::io ; //Mentioning the crate and library which we are using
// The '::' is to mention in which library this crate is
//It can be metioned anywhere except inside the fn main()
fn main() {
let mut a = String::new() ;
io::stdin()
.read_line(&mut a)
.expect("Failed to read line");
println!("A is : {} " , a);
So let's break this down into steps
Step 1 : We add the line use std::io which means that we are going to use the input/output crate .
Step 2 : We assign The Strings::new function to the variable 'a' .
Step 3 : We call the input/output function from the standard library.
Step 4 : We add to statements ,
( i )read_line is a command for waiting and getting the input .
( ii )"expect" is a another method which tells the compiler to expect a error crash .
And finally we print a is: and the value of a given from the user .
Float : A data type
So float is an another data type like an integer .
fn main {
x: f32 = 34.5 ;
}
So a float is basically decimals in rust which are denoted by the keyword : f32 . There are only f32 , f64 for more detail explanation and other different data types please wait for the data types article coming up soon.
Note :
Please kindly check the previous Rust Basics series article for updates now an then.