Created
November 26, 2023 18:44
-
-
Save i-asimkhan/beb7d8fd472e195c9ac2c0f670007fbc to your computer and use it in GitHub Desktop.
Here are the available list of data type in #rust programming language.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[allow(dead_code)] | |
#[allow(unused_variables)] | |
const AVERAGE_AGE : u8 = 60; // no fixed address | |
static PREFIX : char = 'A'; // fixed address | |
static mut AGE : u8 = 22; // mutable fixed address | |
fn main() { | |
data_types(); | |
characters(); | |
floating_point_values(); | |
boolean_values(); | |
} | |
fn data_types() { | |
// u is unsigned, 0 to 2^n-1 | |
let a:u32 = 12; | |
let b: u8 = 123; // u = unsigned, 8 bits, 0 to 255 | |
println!("value of a = {}" ,a); | |
println!("value of b = {}" ,b); // immutable | |
// i is signed, -2^n-1 to (2^n-1)-1 | |
let mut c: i8 = 0; // i = signed, -128 to 127 | |
println!("c = {} before", c); | |
c = 8; | |
println!("c = {} after", c); | |
let mut d = 123456789; // i32 | |
println!("d = {}, takes up {} bytes", d, mem::size_of_val(&d)); | |
d = -1; // i32 | |
println!("d = {}, takes up {} bytes", d, mem::size_of_val(&d)); | |
// available data types | |
// u8, u16, u32, u64, i8, i16, ... | |
/* | |
let u1 : u8 = 0; | |
let u2 : u16 = 0; | |
let u3 : u32 = 0; | |
let u4 : u64 = 0; | |
let u5 : u128 = 0; | |
let i1 : i8 = 0; | |
let i2 : i16 = 0; | |
let i3 : i32 = 0; | |
let i4 : i64 = 0; | |
let i5 : i128 = 0;*/ | |
// usize or isize | |
let z: isize = 1234; | |
let size_of_z = mem::size_of_val(&z); | |
println!("z = {}, takes up {} bytes, {} - bit OS", z, size_of_z, size_of_z*8) | |
} | |
fn characters() { | |
let a: char = 'X'; | |
println!("{} is a char, size = {} bytes", a, mem::size_of_val(&a)); | |
} | |
fn floating_point_values() { | |
// floating point number | |
// f32 or f64 or IEEE754 Standard | |
// All are signed numbers | |
let a: f32 = 2.5; | |
println!("{}, size = {} bytes", a, mem::size_of_val(&a)); | |
} | |
fn boolean_values() { | |
let married: bool = true; | |
println!("{}, size = {} bytes ",married, mem::size_of_val(&married)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment