mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-07-06 11:07:12 -06:00
finished ch3 started on ch4
This commit is contained in:
118
data_types.md
118
data_types.md
@ -120,6 +120,13 @@ value stored 13
|
||||
let l = 0b1101;
|
||||
```
|
||||
|
||||
##### Bytes
|
||||
u8 only
|
||||
value stored 0x41 or 65
|
||||
```rust
|
||||
let m = b'A';
|
||||
```
|
||||
|
||||
## Numeric Operators / Basic Math
|
||||
Numbers for reference
|
||||
```rust
|
||||
@ -158,8 +165,8 @@ Numbers for reference
|
||||
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;
|
||||
let n = false;
|
||||
let o = true;
|
||||
```
|
||||
|
||||
### Char
|
||||
@ -167,8 +174,111 @@ 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';
|
||||
let p = 'a';
|
||||
```
|
||||
|
||||
### Compound Types
|
||||
## Compound Types
|
||||
multiple values into one type
|
||||
|
||||
### Tuple
|
||||
A general way of grouping multiple a number of values into one compound type
|
||||
types do not need to be the same in every position
|
||||
|
||||
```rust
|
||||
let tup: (i32, f64, u8) = (500, 6.4, 1);
|
||||
```
|
||||
The variable tup has values written to it at initialization but it is not requried, order does not matter
|
||||
similar to a struct in c
|
||||
|
||||
Vaules must be destructed out of a tuple to be accessed inidivually, can use a pattern matching to the tuple
|
||||
|
||||
```rust
|
||||
let (q, r, s) = tup;
|
||||
```
|
||||
|
||||
This is called destructing becasue it breaks it into 3 parts
|
||||
|
||||
Can also be accessed with a .
|
||||
|
||||
***INDEX STARTS AT 0***
|
||||
```rust
|
||||
let t = tup.0; // t = 500
|
||||
let u = tup.1; // u = 6.4
|
||||
let v = tup.2; // v = 1
|
||||
```
|
||||
|
||||
#### A Unit
|
||||
|
||||
This is a special value where a tuple has no values
|
||||
```rust
|
||||
let w: () = ();
|
||||
```
|
||||
This represents an empty type or an empty return type
|
||||
|
||||
Expressions will implicitly return a unit if they dont return anything else
|
||||
|
||||
### Array
|
||||
|
||||
A collection of multiple values
|
||||
Must have every value be the same type, cannot mix and match
|
||||
Arrays must be a fixed length at initialization
|
||||
useful when you want a set number of values or is static
|
||||
|
||||
Values are in [] and seperated by ,
|
||||
|
||||
```rust
|
||||
let xa = [1, 2, 3, 4, 5, 6];
|
||||
```
|
||||
|
||||
Array located in stack same with above types
|
||||
|
||||
If you need your array/list to grow or shrink use a vector
|
||||
If unsure weather to use an Array or Vector pick a vector
|
||||
|
||||
Times where using an array is better
|
||||
```rust
|
||||
let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
|
||||
```
|
||||
|
||||
Accessing items in an array
|
||||
```rust
|
||||
let ya = xa[0]; //value is 1
|
||||
```
|
||||
|
||||
Initializing an Array
|
||||
```rust
|
||||
let za: [i32; 5]; // allows for 5 32 bit signed integers inside
|
||||
let aa = [i8; 6]; // allow for 6 8 bit signed integers inside
|
||||
```
|
||||
|
||||
Invalid Array Elements
|
||||
|
||||
```rust
|
||||
use std::io;
|
||||
fn main() {
|
||||
let a = [1, 2, 3, 4, 5];
|
||||
|
||||
// Input of a number
|
||||
println!("Please enter an array index.");
|
||||
let mut index = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut index)
|
||||
.expect("Failed to read line");
|
||||
|
||||
// change into a integer
|
||||
let index: usize = index
|
||||
.trim()
|
||||
.parse()
|
||||
.expect("Index entered was not a number");
|
||||
|
||||
// access elemetn in array
|
||||
let element = a[index];
|
||||
println!("The value of the element at index {index} is: {element}");
|
||||
}
|
||||
```
|
||||
this program would compile with not problems
|
||||
for example inputting 7 into the program
|
||||
this would cause a runtime error
|
||||
the program would output an error because it didnt get to the final line println! before exiting
|
||||
it casue the program to exit before attempting to access the invalid space
|
||||
this is a form of safe memory management that rust name
|
Reference in New Issue
Block a user