mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-06-15 13:04:18 -06:00
finished ch3 started on ch4
This commit is contained in:
parent
2aceb5c3d0
commit
af7f91dce1
118
data_types.md
118
data_types.md
@ -120,6 +120,13 @@ value stored 13
|
|||||||
let l = 0b1101;
|
let l = 0b1101;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
##### Bytes
|
||||||
|
u8 only
|
||||||
|
value stored 0x41 or 65
|
||||||
|
```rust
|
||||||
|
let m = b'A';
|
||||||
|
```
|
||||||
|
|
||||||
## Numeric Operators / Basic Math
|
## Numeric Operators / Basic Math
|
||||||
Numbers for reference
|
Numbers for reference
|
||||||
```rust
|
```rust
|
||||||
@ -158,8 +165,8 @@ Numbers for reference
|
|||||||
must be explicity typed to true or false
|
must be explicity typed to true or false
|
||||||
0 or 1 not allowed even with let var: bool
|
0 or 1 not allowed even with let var: bool
|
||||||
```rust
|
```rust
|
||||||
let m = false;
|
let n = false;
|
||||||
let n = true;
|
let o = true;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Char
|
### 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
|
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
|
takes 4 bytes in size or 32 bits
|
||||||
```rust
|
```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
|
6
functions_rust/Cargo.toml
Normal file
6
functions_rust/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "functions_rust"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
15
functions_rust/src/main.rs
Normal file
15
functions_rust/src/main.rs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
example_function(1, 4); // can be delared before or after function that uses it
|
||||||
|
}
|
||||||
|
|
||||||
|
// fn is the keyword for functions
|
||||||
|
fn example_function (x:i8, y:u16) {
|
||||||
|
// type must be delared cannot be inferred
|
||||||
|
// name of parameter/argument is in the brakets seperated by commas
|
||||||
|
// {} show scope of function
|
||||||
|
|
||||||
|
// can be used directly form pram delaration
|
||||||
|
println!("Look anthoer Function {} {y}", x);
|
||||||
|
|
||||||
|
}
|
@ -1 +1 @@
|
|||||||
{"rustc_fingerprint":15062005826404582409,"outputs":{"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/framework-brock/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.83.0 (90b35a623 2024-11-26)\nbinary: rustc\ncommit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\ncommit-date: 2024-11-26\nhost: x86_64-unknown-linux-gnu\nrelease: 1.83.0\nLLVM version: 19.1.1\n","stderr":""}},"successes":{}}
|
{"rustc_fingerprint":13662911779824945272,"outputs":{"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\nfmt_debug=\"full\"\noverflow_checks\npanic=\"unwind\"\nproc_macro\nrelocation_model=\"pic\"\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"lahfsahf\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_has_atomic_equal_alignment=\"128\"\ntarget_has_atomic_equal_alignment=\"16\"\ntarget_has_atomic_equal_alignment=\"32\"\ntarget_has_atomic_equal_alignment=\"64\"\ntarget_has_atomic_equal_alignment=\"8\"\ntarget_has_atomic_equal_alignment=\"ptr\"\ntarget_has_atomic_load_store\ntarget_has_atomic_load_store=\"128\"\ntarget_has_atomic_load_store=\"16\"\ntarget_has_atomic_load_store=\"32\"\ntarget_has_atomic_load_store=\"64\"\ntarget_has_atomic_load_store=\"8\"\ntarget_has_atomic_load_store=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_thread_local\ntarget_vendor=\"pc\"\nub_checks\nwindows\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.83.0 (90b35a623 2024-11-26)\nbinary: rustc\ncommit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\ncommit-date: 2024-11-26\nhost: x86_64-pc-windows-msvc\nrelease: 1.83.0\nLLVM version: 19.1.1\n","stderr":""},"12744816824612481171":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""}},"successes":{}}
|
@ -1 +1 @@
|
|||||||
{"rustc_fingerprint":15062005826404582409,"outputs":{"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/framework-brock/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.83.0 (90b35a623 2024-11-26)\nbinary: rustc\ncommit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\ncommit-date: 2024-11-26\nhost: x86_64-unknown-linux-gnu\nrelease: 1.83.0\nLLVM version: 19.1.1\n","stderr":""}},"successes":{}}
|
{"rustc_fingerprint":13662911779824945272,"outputs":{"12744816824612481171":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.83.0 (90b35a623 2024-11-26)\nbinary: rustc\ncommit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\ncommit-date: 2024-11-26\nhost: x86_64-pc-windows-msvc\nrelease: 1.83.0\nLLVM version: 19.1.1\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\nfmt_debug=\"full\"\noverflow_checks\npanic=\"unwind\"\nproc_macro\nrelocation_model=\"pic\"\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"lahfsahf\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_has_atomic_equal_alignment=\"128\"\ntarget_has_atomic_equal_alignment=\"16\"\ntarget_has_atomic_equal_alignment=\"32\"\ntarget_has_atomic_equal_alignment=\"64\"\ntarget_has_atomic_equal_alignment=\"8\"\ntarget_has_atomic_equal_alignment=\"ptr\"\ntarget_has_atomic_load_store\ntarget_has_atomic_load_store=\"128\"\ntarget_has_atomic_load_store=\"16\"\ntarget_has_atomic_load_store=\"32\"\ntarget_has_atomic_load_store=\"64\"\ntarget_has_atomic_load_store=\"8\"\ntarget_has_atomic_load_store=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_thread_local\ntarget_vendor=\"pc\"\nub_checks\nwindows\n","stderr":""}},"successes":{}}
|
Loading…
x
Reference in New Issue
Block a user