cpc
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
pub fn run() {
|
||||
println!("=== 2. data types ===");
|
||||
println!("\n=== 2.1 floating points ===\n");
|
||||
|
||||
let x = 2.0; // f64
|
||||
let y: f32 = 3.0; // f32
|
||||
|
||||
println!("delcared floats: \nvar x: {x} \nvar y: {y}");
|
||||
|
||||
|
||||
println!("\n=== 2.2 numeric ops ===\n");
|
||||
// addition
|
||||
let sum = 5 + 10;
|
||||
|
||||
// subtraction
|
||||
let difference = 95.5 - 4.3;
|
||||
|
||||
// multiplication
|
||||
let product = 4 * 30;
|
||||
|
||||
// division
|
||||
let quotient = 56.7 / 32.2;
|
||||
let truncated = -5 / 3; // Results in -1
|
||||
|
||||
// remainder
|
||||
let remainder = 43 % 5;
|
||||
println!("5 + 10: {sum}\n95.5 - 4.3: {difference}\n4 * 30: {product}\n56.7 divided by 32.2: {quotient}\ntruncated -5 / 3: {truncated}\n43 % 5: {remainder}\n");
|
||||
|
||||
|
||||
|
||||
println!("\n=== 2.2 compound types ===\n");
|
||||
// Compound type
|
||||
}
|
||||
@@ -1,3 +1,8 @@
|
||||
mod variables;
|
||||
mod data_types;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
variables::run();
|
||||
println!("\n#####################\n");
|
||||
data_types::run();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
pub fn run() {
|
||||
println!("=== 1. variables ===");
|
||||
// NON Working code, variables are immutable by default
|
||||
// let x = 5;
|
||||
// println!("var x is assigned {x}");
|
||||
//
|
||||
// x = 6;
|
||||
// println!("var x is assigned {x}");
|
||||
|
||||
// This works, because we declared the variable as mutable
|
||||
println!("\n=== 1.1 mutable variables===\n");
|
||||
let mut x = 5;
|
||||
println!("The value of x is: {x}");
|
||||
x = 6;
|
||||
println!("The value of x is: {x}");
|
||||
|
||||
// Declaring constants: constants are declared as const. They can NEVER be mutated. They also
|
||||
// must have a type declaration. Convention is to use all uppercase, and word separators as
|
||||
// underscore.
|
||||
println!("\n=== 1.2 constants ===\n");
|
||||
const THREE_HOURS_IN_MINUTES: u32 = 60 * 60 * 3;
|
||||
println!("Three hours in muntes: {THREE_HOURS_IN_MINUTES}");
|
||||
|
||||
// Variable shadowing
|
||||
// variable shadowing is handy for altering the variables somewhat, like when getting user
|
||||
// input, then doing to length instead of having it as a string. If you do this with mut
|
||||
// variable, then you'll get compile time error. The variable shadowing is great for not having
|
||||
// to constantly creating new variables which only have one final purpose. Like space,
|
||||
// space_count and such.
|
||||
println!("\n=== 1.3 variable shadowing ===\n");
|
||||
let y = 5;
|
||||
let y = y + 1;
|
||||
|
||||
{
|
||||
let y = y * 3;
|
||||
println!("The value of y in the inner scope is {y}");
|
||||
}
|
||||
|
||||
println!("The value of y is {y}");
|
||||
}
|
||||
Reference in New Issue
Block a user