fn main() {
// immutable
let x = 5;
println!("The value of x is: {}", x);
// x = 7; // connot assign twice to immutable variable
println!("The value of x is: {}", x);
// shadowing
let x = 10;
println!("The value of x is: {}", x);
// shadowing 2
let spaces = " ";
println!("Spaces: {}", spaces);
let spaces = spaces.len();
println!("Spaces: {}", spaces);
// mutable
let mut x = 6;
println!("The value of x is: {}", x);
x = 7; // connot assign twice to immutable variable
println!("The value of x is: {}", x);
// mutable
let mut spaces = " ";
println!("Spaces: {}", spaces);
// spaces = spaces.len(); //expected `&str`, found `usize`
println!("Spaces: {}", spaces);
// const
const MAX_POINTS: u32 = 100_000;
println!("MAX_POINTS: {}", MAX_POINTS);
// let guess = "42".parse().expect("Not a number!"); // consider giving `guess` a type
let guess: u32 = "42".parse().expect("Not a number!");
// float
let f1: f64 = 2.0;
let f2: f32 = 3.0;
println!("{},{}", f1, f2);
// let addition
let sum = 5 + 10;
// let subtraction
let difference = 95.5 - 4.3;
// let multiplication
let product = 4 * 30;
// let division
let quotient = 56.7 / 32.2;
// let remainder
let remainder = 43 % 5;
println!(
"let calc {},{},{},{},{}",
sum, difference, product, quotient, remainder
);
// boolean
let t = true;
let f: bool = false;
println!("let bool {},{}", t, f);
// character
let c = 'z';
let z = 'ℤ';
let cat = '😻';
println!("let char {},{},{}", c, z, cat);
// tuple
let tup: (i32, f64, u8) = (500, 6.4, 1);
let (x, y, z) = tup;
println!("x : {}, y : {}, z : {}", x, y, z);
let x: (i32, f64, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8) =
(500, 6.4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
let five_hundred = x.0;
println!("five_hundred : {}, x11 : {}", five_hundred, x.11);
// Array
let ary = [1, 2, 3, 4, 5];
let mon = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
let index = 4;
// let index = 12; // panicked at 'index out of bounds: the len is 12 but the index is 12'
let frst = ary[0];
let scnd = mon[1];
let thrd = mon[index];
println!("frst : {} , scnd : {} , thrd : {}", frst, scnd, thrd);
}
comments powered by Disqus