diff --git a/code/02_data_types/01_integers/integer_vars/src/ints_info.rs b/code/02_data_types/01_integers/integer_vars/src/ints_info.rs new file mode 100644 index 0000000..1b197e0 --- /dev/null +++ b/code/02_data_types/01_integers/integer_vars/src/ints_info.rs @@ -0,0 +1,109 @@ +#![allow(dead_code)] +use crate::banner; +use std::any::type_name; + +fn type_of(_: T) -> &'static str { + type_name::() +} + +pub fn ints_types() { + let int8: i8 = -42; + let int16: i16 = -32000; + let int32: i32 = -2_000_000; + let int64: i64 = -9_000_000_000; + let int128: i128 = -9_000_000_000_000; + + banner("=", 62, "Int8 Integer Types in Rust"); + println!( + "Decimal int8: {},\nBinary int8: {:b},\nOctal int8: {},\nHexadecimal int8: {:x}", + int8, int8, int8, int8 + ); + + banner("=", 62, "Int16 Integer Types in Rust"); + println!( + "Decimal int16: {},\nBinary int16: {:b},\nOctal int16: {},\nHexadecimal int16: {:x}", + int16, int16, int16, int16 + ); + + banner("=", 62, "Int32 Integer Types in Rust"); + println!( + "Decimal Int32: {},\nBinary int32: {:b},\nOctal int32: {},\nHexadecimal int32: {:x}", + int32, int32, int32, int32 + ); + + banner("=", 62, "Int64 Integer Types in Rust"); + println!( + "Decimal int64: {},\nBinary int64: {:b},\nOctal int64: {},\nHexadecimal int64: {:x}", + int64, int64, int64, int64 + ); + + banner("=", 62, "Int128 Integer Types in Rust"); + println!( + "Decimal int128: {},\nBinary int128: {:b},\nOctal int128: {},\nHexadecimal int128: {:x}", + int128, int128, int128, int128 + ); +} + +pub fn ints_info() { + let int8: i8 = -42; + let int16: i16 = -32000; + let int32: i32 = -2_000_000; + let int64: i64 = -9_000_000_000; + let int128: i128 = -9_000_000_000_000; + + banner("=", 62, "Integer Types in Rust, Types and Size in Memory"); + + println!( + "int8: {:<15}, type: {:<10}, size: {} bytes", + int8, + type_of(int8), + std::mem::size_of::() + ); + println!( + "int16: {:<15}, type: {:<10}, size: {} bytes", + int16, + type_of(int16), + std::mem::size_of::() + ); + println!( + "int32: {:<15}, type: {:<10}, size: {} bytes", + int32, + type_of(int32), + std::mem::size_of::() + ); + println!( + "int64: {:<15}, type: {:<10}, size: {} bytes", + int64, + type_of(int64), + std::mem::size_of::() + ); + println!( + "int128: {:<15}, type: {:<10}, size: {} bytes", + int128, + type_of(int128), + std::mem::size_of::() + ); +} + +pub fn min_max_ints() { + banner("*", 72, "The MIN and Max Values for Each Integer Data Type"); + // Print min and max values for each type + print_data_type_info::("i8", i8::MIN, i8::MAX); + print_data_type_info::("i16", i16::MIN, i16::MAX); + print_data_type_info::("i32", i32::MIN, i32::MAX); + print_data_type_info::("i64", i64::MIN, i64::MAX); + print_data_type_info::("i128", i128::MIN, i128::MAX); +} + +fn print_data_type_info(type_name: &str, min: T, max: T) +where + T: std::fmt::Display, +{ + println!( + "Type: {:<8}, Size: {:<8} bytes, Min: {:<25}, Max: {}", + type_name, + std::mem::size_of::(), + min, + max + ); +} diff --git a/code/02_data_types/01_integers/integer_vars/src/main.rs b/code/02_data_types/01_integers/integer_vars/src/main.rs index fc04df7..72027d2 100644 --- a/code/02_data_types/01_integers/integer_vars/src/main.rs +++ b/code/02_data_types/01_integers/integer_vars/src/main.rs @@ -1,36 +1,50 @@ +//================================================================================ +// Author: Dr. Saad Laouadi +// Course: Rust Distilled +// Chapter 02: Primitive Data Types +// Section: Numeric Data +// lesson: Integers +// +// © copy-right: Dr. Saad Laouadi +//================================================================================ + mod int_operations; +mod ints_info; mod rnd_integers; +mod unsigned_ints; fn main() { // Invoke the print_signed_integers() function - make_separator(); + banner("*", 52, "Integers in Rust"); print_signed_integers(); - make_separator(); + make_separator(52); signed_integers_size(); - make_separator(); + make_separator(52); // Invoke unsigned functions print_unsigned_integers(); - make_separator(); + make_separator(52); unsigned_integers_size(); - make_separator(); + banner("*", 52, "Generate Random Integers"); // Generate a random signed integers rnd_integers::print_rnd_integers(); // Integer Operations - make_separator(); + make_separator(52); int_operations::add_ints(); int_operations::sub_ints(); int_operations::mul_ints(); int_operations::divide_ints(); int_operations::remainder_ints(); - make_separator(); -} + banner("*", 52, "Detailed Information about Integers in Rust;;;"); + ints_info::ints_types(); + + ints_info::ints_info(); + + ints_info::min_max_ints(); -// Simple function that prints `=` to pretify the console output -fn make_separator() { - println!("{}", "*".repeat(52)); + unsigned_ints::get_unsigned_ints_info(); } // a function that prints signed integers @@ -114,3 +128,15 @@ fn unsigned_integers_size() { std::mem::size_of::() ); } + +// Function to create a formatted banner +fn banner(sep: &str, nchar: usize, message: &str) { + let sep = sep.repeat(nchar); + let message = format!("{:^width$}", message, width = nchar); + println!("\n{}\n{}\n{}", sep, message, sep); +} + +// make separator function +fn make_separator(nchar: usize) { + println!("{}", "*".repeat(nchar)) +} diff --git a/code/02_data_types/01_integers/integer_vars/src/rnd_integers.rs b/code/02_data_types/01_integers/integer_vars/src/rnd_integers.rs index 4c425da..a6b622a 100644 --- a/code/02_data_types/01_integers/integer_vars/src/rnd_integers.rs +++ b/code/02_data_types/01_integers/integer_vars/src/rnd_integers.rs @@ -1,4 +1,6 @@ use rand; + +use rand::Rng; // Advanced Section // ================ @@ -6,8 +8,9 @@ pub fn generate_random_signed_integer() -> T where rand::distributions::Standard: rand::distributions::Distribution, { - rand::random() + rand::thread_rng().gen() } + // Print random generated signed integers pub fn print_rnd_integers() { let random_integer_i8: i8 = generate_random_signed_integer(); diff --git a/code/02_data_types/01_integers/integer_vars/src/unsigned_ints.rs b/code/02_data_types/01_integers/integer_vars/src/unsigned_ints.rs new file mode 100644 index 0000000..3108ea1 --- /dev/null +++ b/code/02_data_types/01_integers/integer_vars/src/unsigned_ints.rs @@ -0,0 +1,39 @@ +use crate::banner; + +pub fn get_unsigned_ints_info() { + let uint8: u8 = 42; + let uint16: u16 = 32000; + let uint32: u32 = 2_000_000; + let uint64: u64 = 9_000_000_000; + let uint128: u128 = 9_000_000_000_000; + + banner("=", 62, "uint8 Integer Types in Rust"); + println!( + "Decimal uint8: {},\nBinary uint8: {:b},\nOctal uint8: {},\nHexadecimal: {:x}", + uint8, uint8, uint8, uint8 + ); + + banner("=", 62, "uint16 Integer Types in Rust"); + println!( + "Decimal uint16: {},\nBinary uint16: {:b},\nOctal uint16: {},\nHexadecimal uint16: {:x}", + uint16, uint16, uint16, uint16 + ); + + banner("=", 62, "uint32 Integer Types in Rust"); + println!( + "Decimal uint32: {},\nBinary uint32: {:b},\nOctal uint32: {},\nHexadecimal uint32: {:x}", + uint32, uint32, uint32, uint32 + ); + + banner("=", 62, "uint64 Integer Types in Rust"); + println!( + "Decimal uint64: {},\nBinary uint64: {:b},\nOctal uint64: {},\nHexadecimaluint64: {:x}", + uint64, uint64, uint64, uint64 + ); + + banner("=", 62, "Int128 Integer Types in Rust"); + println!( + "Decimal int128: {},\nBinary uint128: {:b},\nOctal uint128: {},\nHexadecimal uint128: {:x}", + uint128, uint128, uint128, uint128 + ); +}