mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-07-06 11:07:12 -06:00
start of ch 3
This commit is contained in:
174
data_types.md
Normal file
174
data_types.md
Normal file
@ -0,0 +1,174 @@
|
||||
|
||||
# Data Types
|
||||
|
||||
## Constants
|
||||
|
||||
constants can be delared anywhere, convension to use all caps in const
|
||||
need const keyword, not always eval at compile time
|
||||
variables can only be assigned once (needs mut to assign more than once (need to be same type))
|
||||
|
||||
```rust
|
||||
const SECONDS_PER_HOUR: i32 = 60 * 60;
|
||||
```
|
||||
|
||||
## Variables
|
||||
|
||||
variables are immuatable by default
|
||||
variables can be inferred but sometimes needs explicit typing
|
||||
|
||||
```rust
|
||||
let foo = 5;
|
||||
```
|
||||
need to add mut keyword to enable rewriting, generally avoid unless actually used
|
||||
|
||||
```rust
|
||||
let mut bar = 6;
|
||||
```
|
||||
|
||||
# SHADOWING
|
||||
|
||||
#### Cannot have mutable shadows
|
||||
|
||||
allows for reuse of namespace instead of spaces_str and spaces_num
|
||||
```rust
|
||||
let spaces = " _ _ ";
|
||||
let spaces = spaces.len();
|
||||
// will output 5 instead of " _ _ " beacuse that is how long it is
|
||||
// the shadow of spaces (first) wont be printed until the overshadow of spaces goes out of scope
|
||||
println!("{spaces}"); // output: 5
|
||||
```
|
||||
|
||||
not allowed shadow
|
||||
```rust
|
||||
let mut spaces = " _ _ ";
|
||||
spaces = spaces.len();
|
||||
```
|
||||
cannot change type of variable once declared
|
||||
|
||||
# Primitive Data Types
|
||||
|
||||
## Scalars
|
||||
|
||||
### Integers
|
||||
|
||||
u is for usigned integers
|
||||
i is for signed integers
|
||||
|
||||
number indicated how many bits it takes in memory
|
||||
```rust
|
||||
let z: i8; // takes up 8 bits, can store values from -128 to 127
|
||||
let c: i16; // takes up 16 bits
|
||||
let d: i32; // takes up 32 bits (default for integers)
|
||||
let e: i64; // takes up 64 bits
|
||||
let f: i128; // takes up 128 bits
|
||||
let g: isize; // takes up x bits, depends on the system's architecture/cpu
|
||||
|
||||
let h: u8; // takes up 8 bits, unsigned version (only positive)
|
||||
// can store values from 0 to 255
|
||||
```
|
||||
|
||||
#### Integer Overflow
|
||||
will reset to the lowest value ie i8 129 -> -126
|
||||
```rust
|
||||
let example_over_flow: i8 = 129;
|
||||
```
|
||||
|
||||
behavor only in production mode
|
||||
dev mode will cause a panic and error out/tell you
|
||||
|
||||
### Floats
|
||||
better to use double point due to modern cpus where there is not much difference in speed
|
||||
|
||||
#### Single Point Float
|
||||
takes up 32 bits
|
||||
```rust
|
||||
let a: f32 = 4.0;
|
||||
```
|
||||
|
||||
#### Double Point Float
|
||||
takes up 64 bits
|
||||
```rust
|
||||
let b: f64 = 2.01;
|
||||
```
|
||||
|
||||
#### Integers Represented Differently
|
||||
can represent values in hex, oct, bin or dec
|
||||
can hover with rust-analyzer extension to see value in dec
|
||||
|
||||
##### Dec with Reading Aid
|
||||
value stored 1000
|
||||
_ used to make easier to read
|
||||
```rust
|
||||
let i = 1_000;
|
||||
```
|
||||
|
||||
##### Hexidecimal
|
||||
value stored 255
|
||||
```rust
|
||||
let j = 0xff;
|
||||
```
|
||||
|
||||
##### Octal
|
||||
value stored 63
|
||||
```rust
|
||||
let k = 0o77;
|
||||
```
|
||||
|
||||
##### Binary
|
||||
value stored 13
|
||||
```rust
|
||||
let l = 0b1101;
|
||||
```
|
||||
|
||||
## Numeric Operators / Basic Math
|
||||
Numbers for reference
|
||||
```rust
|
||||
let x: i16 = 8;
|
||||
let y: i16 = 5;
|
||||
```
|
||||
|
||||
### Addition
|
||||
```rust
|
||||
let sum = x + y; // result: 13
|
||||
```
|
||||
|
||||
### Subtraction
|
||||
```rust
|
||||
let difference = x - y; //result: 3
|
||||
```
|
||||
|
||||
### Multiplication
|
||||
```rust
|
||||
let product: i16;
|
||||
product = x * y;
|
||||
```
|
||||
|
||||
### Division
|
||||
```rust
|
||||
let quotent = 45.1 / 54.2;
|
||||
let truncated = x / y; // results in 1 (always rounds down)
|
||||
```
|
||||
|
||||
### Remainder
|
||||
```rust
|
||||
let remainder = x % y;
|
||||
```
|
||||
|
||||
### Booleans
|
||||
must be explicity typed to true or false
|
||||
0 or 1 not allowed even with let var: bool
|
||||
```rust
|
||||
let m = false;
|
||||
let n = true;
|
||||
```
|
||||
|
||||
### Char
|
||||
must use single quotes and not "" otherwise will be inferred as string literal
|
||||
is stored as Unicode Scalar Value allowing for emoji, japanse char and other languages not supported by ASCII
|
||||
takes 4 bytes in size or 32 bits
|
||||
```rust
|
||||
let o = 'a';
|
||||
```
|
||||
|
||||
### Compound Types
|
||||
|
Reference in New Issue
Block a user