Skip to content

Commit

Permalink
Update chap 02 code
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-saad-la committed Jul 20, 2024
1 parent 27b0ef2 commit 418c532
Show file tree
Hide file tree
Showing 36 changed files with 1,001 additions and 36 deletions.
75 changes: 75 additions & 0 deletions code/02_data_types/01_integers/integer_vars/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions code/02_data_types/01_integers/integer_vars/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "integer_vars"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "*"
34 changes: 34 additions & 0 deletions code/02_data_types/01_integers/integer_vars/src/int_operations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Function to add two integers and return the result.
pub fn add_ints() {
let a = 10;
let b = 5;
println!("The sum of two ints is {}", a + b)
}

// Function to subtract the second integer from the first and return the result.
pub fn sub_ints() {
let a = 10;
let b = 5;
println!("The subtraction result is {}", a - b)
}

// Function to multiply two integers and return the result.
pub fn mul_ints() {
let a = 10;
let b = 5;
println!("The multiplication results is {}", a * b)
}

// Function to divide the first integer by the second and return the result.
pub fn divide_ints() {
let a = 7;
let b = 3;
println!("The integer division result is {}", (a / b))
}

// Function to calculate the remainder when the first integer is divided by the second.
pub fn remainder_ints() {
let a = 7;
let b = 3;
println!("The remainder of the division is {}", (a % b));
}
116 changes: 116 additions & 0 deletions code/02_data_types/01_integers/integer_vars/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
mod int_operations;
mod rnd_integers;

fn main() {
// Invoke the print_signed_integers() function
make_separator();
print_signed_integers();
make_separator();
signed_integers_size();
make_separator();

// Invoke unsigned functions
print_unsigned_integers();
make_separator();
unsigned_integers_size();
make_separator();

// Generate a random signed integers
rnd_integers::print_rnd_integers();

// Integer Operations
make_separator();
int_operations::add_ints();
int_operations::sub_ints();
int_operations::mul_ints();
int_operations::divide_ints();
int_operations::remainder_ints();
make_separator();
}

// Simple function that prints `=` to pretify the console output
fn make_separator() {
println!("{}", "*".repeat(52));
}

// a function that prints signed integers
fn print_signed_integers() {
let i8_val: i8 = -123;
let i16_val: i16 = -12345;
let i32_val: i32 = -12345678;
let i64_val: i64 = -1234567890;

println!("i8 : {}", i8_val);
println!("i16: {}", i16_val);
println!("i32: {}", i32_val);
println!("i64: {}", i64_val);
}

fn signed_integers_size() {
let i8_val: i8 = -123;
let i16_val: i16 = -12345;
let i32_val: i32 = -12345678;
let i64_val: i64 = -1234567890;

println!(
"i8 : {:<12} (Size: {} bytes)",
i8_val,
std::mem::size_of::<i8>()
);
println!(
"i16: {:<12} (Size: {} bytes)",
i16_val,
std::mem::size_of::<i16>()
);
println!(
"i32: {:<12} (Size: {} bytes)",
i32_val,
std::mem::size_of::<i32>()
);
println!(
"i64: {:<12} (Size: {} bytes)",
i64_val,
std::mem::size_of::<i64>()
);
}

// A function that print unsigned integers
fn print_unsigned_integers() {
let u8_val: u8 = 123;
let u16_val: u16 = 12345;
let u32_val: u32 = 12345678;
let u64_val: u64 = 1234567890;

println!("u8: {}", u8_val);
println!("u16: {}", u16_val);
println!("u32: {}", u32_val);
println!("u64: {}", u64_val);
}

fn unsigned_integers_size() {
let u8_val: u8 = 123;
let u16_val: u16 = 12345;
let u32_val: u32 = 12345678;
let u64_val: u64 = 1234567890;

println!(
"u8: {:<12} (Size: {} bytes)",
u8_val,
std::mem::size_of::<u8>()
);
println!(
"u16: {:<12} (Size: {} bytes)",
u16_val,
std::mem::size_of::<u16>()
);
println!(
"u32: {:<12} (Size: {} bytes)",
u32_val,
std::mem::size_of::<u32>()
);
println!(
"u64: {:<12} (Size: {} bytes)",
u64_val,
std::mem::size_of::<u64>()
);
}
22 changes: 22 additions & 0 deletions code/02_data_types/01_integers/integer_vars/src/rnd_integers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use rand;
// Advanced Section
// ================

pub fn generate_random_signed_integer<T>() -> T
where
rand::distributions::Standard: rand::distributions::Distribution<T>,
{
rand::random()
}
// Print random generated signed integers
pub fn print_rnd_integers() {
let random_integer_i8: i8 = generate_random_signed_integer();
let random_integer_i16: i16 = generate_random_signed_integer();
let random_integer_i32: i32 = generate_random_signed_integer();
let random_integer_i64: i64 = generate_random_signed_integer();

println!("Random i8 : {}", random_integer_i8);
println!("Random i16: {}", random_integer_i16);
println!("Random i32: {}", random_integer_i32);
println!("Random i64: {}", random_integer_i64);
}
75 changes: 75 additions & 0 deletions code/02_data_types/02_floats/float_vars/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions code/02_data_types/02_floats/float_vars/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "float_vars"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "*"
Binary file added code/02_data_types/02_floats/float_vars/src/main
Binary file not shown.
Loading

0 comments on commit 418c532

Please sign in to comment.