v0.0.2 chap3.2 datatypes scalar
This commit is contained in:
parent
b5680df6c1
commit
bb80384d53
1
datatypes/.gitignore
vendored
Normal file
1
datatypes/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
7
datatypes/Cargo.lock
generated
Normal file
7
datatypes/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "datatypes"
|
||||||
|
version = "0.1.0"
|
||||||
6
datatypes/Cargo.toml
Normal file
6
datatypes/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "datatypes"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
52
datatypes/src/main.rs
Normal file
52
datatypes/src/main.rs
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
fn main() {
|
||||||
|
println!("
|
||||||
|
Rust is a statically typed language.
|
||||||
|
|
||||||
|
There are 2 subsets of datatypes: scalar and compound.
|
||||||
|
|
||||||
|
1/ Scalar types
|
||||||
|
|
||||||
|
Scalar types represents a single value.
|
||||||
|
|
||||||
|
Rust has: integers, floating-point numbers,
|
||||||
|
Boolean and characters
|
||||||
|
|
||||||
|
1.1/ integers can be signed or unsigned and
|
||||||
|
size from 8 bits to 128 bits,
|
||||||
|
or have a size depending of the arch.
|
||||||
|
Default type is i32.
|
||||||
|
i8 ranges from -(2⁷) to 2⁷ - 1, ie -128 to 127
|
||||||
|
|
||||||
|
let x: u32 = 3000;
|
||||||
|
let x: i32 = -230_000; // equals -230000
|
||||||
|
let x: u16 = 0xff; // hexa form for 255
|
||||||
|
let x: usize = 0b1111_0000; // binary form
|
||||||
|
let x: u8 = b'A'; // byte form only u8
|
||||||
|
|
||||||
|
1.2/ Floating-point types can be f32 or f64,
|
||||||
|
default to f64:
|
||||||
|
|
||||||
|
let x = 2.0; // f64
|
||||||
|
let x: f32 = 3.0; // f32
|
||||||
|
|
||||||
|
Support basic mathematical operations:
|
||||||
|
|
||||||
|
let sum = 3.4 + 5.6;
|
||||||
|
let difference = 90_000 - 289;
|
||||||
|
let product = 4 * 30;
|
||||||
|
let quotient = 56.7 / 32.2;
|
||||||
|
let truncated = -5 / 3;
|
||||||
|
let remainder = 43 % 5;
|
||||||
|
|
||||||
|
1.3/ Boolean:
|
||||||
|
|
||||||
|
let t: bool = true;
|
||||||
|
|
||||||
|
1.4/ A `char` is 4 bytes in rust and covers unicode:
|
||||||
|
|
||||||
|
let mut c: char = 'z' // note the single quote
|
||||||
|
c = '🤗'
|
||||||
|
|
||||||
|
");
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user