Rust
Rust Notes
Understanding Main function and Macros of Rust using Hello World.
// This is a comment
fn main() {
println!("Hello, world!");
// This prints hello, world with a \n by default
}
fn
: funcation (like mini program)main
: the main functon, where the program begins- macro: (like built in function). How is it a macro? ->
!
print!
: just a normal print. all output of multipleprint!
will be in one line without spacesprintln!
: Prints eachprintln!
strings in different lines
- Comments:
//
Comments
// This is a comment
fn main() {
println!("Hello, world!");
/*
This is a
multi - line
comment
block. (/* xyz */)
*/
}
Errors
fn main() {
println!('Hello, world!');
}
- In rust, when we want to declare a string, it needs to be in single quatation
"xyz"
, not in single quatation.
Premitives: Integer
- Scalar types: int, char, foat, boolean
- unsigned: never -ve (u8, u16, u32, u64, u128, usize)
- signed: can be -ve or +ve (i8, i16, i32, i64, i128, isize)
- Default is i32 (best practice)
// {} -> Place holder
// MAX -> Max value that can be represented by this integer type
fn main() {
println!("Max size of a u32: {}", u32::MAX);
}
Output: Max size of a u32: 4294967295
Premitives: Float
- Floats: f32, f64
- pi = 3.14
fn main() {
println!("Max size of a f32: {}", f32::MAX);
println!("Max size of a f64: {}", f64::MAX);
}
// Max size of a f32: 340282350000000000000000000000000000000
// Max size of a f64: 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Premitives: boolean
- True or False
- representation: bool
Premitives: character
- char - 4 bytes
Variables
- let
- variables in rust are immutable
fn main() {
let hello = "Hello, world!";
// let hello: &str = "Hello, world!";
// let _hello = "Hello, world!";
// unused variable: `hello`, mute output
println!("{}", hello);
}
- Mutable variable
fn main() {
let hello = "Hello, world!";
println!("{}", hello);
}
More content soon!