Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-saad-la committed Jul 20, 2024
1 parent 1a27682 commit 5d0cbb9
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::banner;
use std::any::type_name;

fn type_of<T>(_: T) -> &'static str {
pub fn type_of<T>(_: T) -> &'static str {
type_name::<T>()
}

Expand Down
5 changes: 4 additions & 1 deletion code/02_data_types/01_integers/integer_vars/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ fn main() {
int_operations::mul_ints();
int_operations::divide_ints();
int_operations::remainder_ints();

banner("*", 52, "Detailed Information about Integers in Rust;;;");
ints_info::ints_types();

ints_info::ints_info();

ints_info::min_max_ints();

// Unsigned Integers
unsigned_ints::get_unsigned_ints_info();

unsigned_ints::unsigned_type_and_memsize();
}

// a function that prints signed integers
Expand Down
114 changes: 114 additions & 0 deletions code/02_data_types/01_integers/integer_vars/src/unsigned_ints.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::banner;
use crate::ints_info::type_of;

pub fn get_unsigned_ints_info() {
let uint8: u8 = 42;
Expand Down Expand Up @@ -37,3 +38,116 @@ pub fn get_unsigned_ints_info() {
uint128, uint128, uint128, uint128
);
}

pub fn unsigned_type_and_memsize() {
banner(
"=",
62,
"Ints ISize Integer Types in Rust, Types and Size in Memory",
);

let signed_index: isize = -10;
println!(
"signed_index: {}, type: isize, size: {} bytes",
signed_index,
std::mem::size_of::<isize>()
);

// Example of usize
let unsigned_index: usize = 10;
println!(
"unsigned_index: {}, type: usize, size: {} bytes",
unsigned_index,
std::mem::size_of::<usize>()
);

banner(
"=",
62,
"Int Size Integer Types in Rust, Types and Size in Memory",
);
let intsize_1: isize = -9; // Size depends on the architecture (32-bit or 64-bit)
let intsize_2: isize = -9_000;
let intsize_3: isize = -9_000_000;
let intsize_4: isize = -9_000_000_000;
let intsize_5: isize = -9_000_000_000_000_000_000;

println!(
"intsize: {:<15}, type: {:<10}, size: {} bytes",
intsize_1,
type_of(intsize_1),
std::mem::size_of::<isize>()
);
println!(
"intsize: {:<15}, type: {:<10}, size: {} bytes",
intsize_2,
type_of(intsize_2),
std::mem::size_of::<isize>()
);
println!(
"intsize: {:<15}, type: {:<10}, size: {} bytes",
intsize_3,
type_of(intsize_3),
std::mem::size_of::<isize>()
);
println!(
"intsize: {:<15}, type: {:<10}, size: {} bytes",
intsize_4,
type_of(intsize_4),
std::mem::size_of::<isize>()
);
println!(
"intsize: {:<15}, type: {:<10}, size: {} bytes",
intsize_5,
type_of(intsize_5),
std::mem::size_of::<isize>()
);

banner(
"=",
62,
"Int Size Integer Types in Rust, Types and Size in Memory",
);
let untsize_1: usize = 9; // Size depends on the architecture (32-bit or 64-bit)
let untsize_2: usize = 9_000;
let untsize_3: usize = 9_000_000;
let untsize_4: usize = 9_000_000_000;
let untsize_5: usize = 9_000_000_000_000_000_000;

println!(
"intsize: {:<15}, type: {:<10}, size: {} bytes",
untsize_1,
type_of(intsize_1),
std::mem::size_of::<usize>()
);
println!(
"intsize: {:<15}, type: {:<10}, size: {} bytes",
untsize_2,
type_of(intsize_2),
std::mem::size_of::<usize>()
);
println!(
"intsize: {:<15}, type: {:<10}, size: {} bytes",
untsize_3,
type_of(intsize_3),
std::mem::size_of::<usize>()
);
println!(
"intsize: {:<15}, type: {:<10}, size: {} bytes",
untsize_4,
type_of(intsize_4),
std::mem::size_of::<usize>()
);
println!(
"intsize: {:<15}, type: {:<10}, size: {} bytes",
untsize_5,
type_of(intsize_5),
std::mem::size_of::<usize>()
);

banner("=", 62, "Case usage of Usize Type");
// Using usize for array indexing
let array = [1, 2, 3, 4, 5];
let index: usize = 2;
println!("Element at index {}: {}", index, array[index]);
}

0 comments on commit 5d0cbb9

Please sign in to comment.