Compare commits

..

15 Commits

Author SHA1 Message Date
7d92f735c9 starting over 2025-06-25 00:30:17 +00:00
b8f94f79aa erroneous file 2025-06-21 01:13:09 +00:00
aea4ade81b exercism enums basic 1 2023-01-10 08:30:19 -05:00
804501654b started exercism also 2022-12-22 14:24:20 -05:00
8f32bb41ba finally finished slice... 2022-12-20 09:14:10 -05:00
9bca80091f distractions 2022-12-17 09:06:05 -05:00
da9b8386e9 started slices 2022-12-16 16:41:55 -05:00
1f1ea6de94 ownership, references 2022-12-16 16:26:46 -05:00
9a7a9558b5 comments, flow control 2022-12-16 10:00:30 -05:00
6db7d9edf7 added notes did more stuff 2022-12-15 12:25:57 -05:00
055e0d69ae handle errors 2022-12-13 09:09:30 -05:00
ddd68240e7 loop and break 2022-12-13 08:59:43 -05:00
7cbfe8dbc9 add rand, ordering, shadowing, type coercion 2022-12-13 08:53:41 -05:00
915ba1a499 first bit of guessing game ch2 2022-12-12 08:42:20 -05:00
590c683d98 add ignore 2022-12-12 08:41:12 -05:00
6 changed files with 248 additions and 13 deletions

14
.gitignore vendored Normal file
View File

@@ -0,0 +1,14 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

View File

@@ -1,2 +0,0 @@
Cargo.lock
target

View File

@@ -1,8 +0,0 @@
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@@ -1,3 +0,0 @@
fn main() {
println!("ǰ DZ Dz dz Ǵ ǵ Ƕ Ƿ Ǹ ǹ Ǻ ǻ Ǽ ǽ Ǿ");
}

233
notes Normal file
View File

@@ -0,0 +1,233 @@
const is a thing
const convention is all upper with _
const evaluation info: https://doc.rust-lang.org/reference/const_eval.html
you can add arbitray scopes to modify local variables or for other purposes!
{
some random shit;
}
https://doc.rust-lang.org/book/ch03-02-data-types.html
scalars:
ints
floats
bools
chars
integer types:
8-bit i8 u8
16-bit i16 u16
32-bit i32 u32
64-bit i64 u64
128-bit i128 u128
arch isize usize
number literals
decimal 1_024
hex 0xff
oct 0o77
binary 0b1100_1011_0100_0001
byte b'E'
release builds have no overflow wrap checking
don't rely on this behavior
explicitly use
`wrapping_*` methods to do this
such as wrapping_add
Return the None value if there is overflow with the checked_* methods
Return the value and a boolean indicating whether there was overflow with the overflowing_* methods
wtf is this:
Saturate at the values minimum or maximum values with saturating_* methods
there are f32 and f64 floats. f64 is default cause they are about the same speed
normal math operators
int division is floor
remainder % exists
all operators:
https://doc.rust-lang.org/book/appendix-02-operators.html
type annotation:
let var: type = val;
Rusts char type is four bytes in size and represents a Unicode Scalar Value
Compound types
primitive compound types:
tuples
arrays
Tuples:
fixed length combo of types
type is ()
pulling out the elements of a tuple into individual variables is called destructuring
Arrays:
fixed type fixed length
[]
init:
let a: [1, 2, 3];
let a: [3, 0]; // set every element to same value
reference:
let first = a[0];
they are allocated on stack
https://doc.rust-lang.org/book/ch03-03-how-functions-work.html
all function parameters must be annotated
functions consist of statements and expressions
statements perform an action and have no return value
expressions do return something
a scope block is an expression
expressions do not end with a ";", if you add a ";" it will be statement
return is explicit if the last statement in a function is an expression
https://doc.rust-lang.org/book/ch03-04-comments.html
comments
// is a comment
/* is also a comment
b*/
for some reason the doc didn't mention the second type..
https://doc.rust-lang.org/book/ch03-05-control-flow.html
control flow
there is `if``
block of code within the "if" are _arms_.
just like the match
if must recieve a bool
no bad python
if num {}
if (thing) {
else if (other thing) {
} else if (yat) {
} else {
}
use `match` for too many else ifs
if is an expression!
you can loop with `loop`
loop is an expression!
break can return a value (not for for loops though they are a statement)
break can select nest level with 'label
thats called a loop label
while loop exists
for loop exists
syntax: for x in y {}
(start..end) is a Range
(1..4).rev() is a thing
https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html?search=
ownership
stack - lifo in order memory storage, stack of PLATES
pushing / popping
all data stored here must have a FIXED size
heap - rando shit goes here
allocating / deallocating
pointer to data structure goes on the stack, the actual data goes on the heap
each value in Rust has an _owner_
there can be only one owner at a time
when the owner goes out of scope, the value goes out of scope
a string has 3 parts
name | val
-----------
ptr | ------> index | val
len | 5 | -------------
cap | 5 | | 0 | 'h' |
----------- | 1 | 'e' |
string2 = string1;
create a new string data structure
have the pointer point to the same data as string1
invalid:
s2 = s1;
println!("{s1}");
there can't be two pointers to the same value!
shallow copy is a move because the first pointer is invalidated
clone is "deep copy"
s2 = s1.clone();
stack based vars don't need to clone, they `copy`, it's automatic
drop is called to return memory from the heap when it's owner goes out of scope
any type that implements the copy trait will auto copy.
a type can not implement copy and drop...
tuples will copy if all of their types support it, it will not if any one of them does not
a function can take an immutable heap mounted variable and "recast" it is mutable
https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html
references
rust has a method to let a function use a variable without moving / reassigning
ownership: references
& is a reference
* is a dereference
pass a reference to avoid _moving_ the value
references are immutable by default
&mut var // makes a mutable reference
there can be one or multiple r/o references or only one mutable reference at a time
https://doc.rust-lang.org/book/ch04-03-slices.html
slices
reference to a contiguous sequence of elements
enumerate returns a tuple with counter and reference to thing (counter, &thing)
slice is a pointer to a string, with a length
let s = String::from("a a a a");
slice = &s[0..2];
you can drop the first number if it's zero: let slice = &s[..2];
or the last number if it's the end of the string: let slice = &s[4..];
or both numbers to slice the whole thing: let slice = &s[..];
you can not create a slice that starts or ends within multibyte unicode parts
String: a heap allocated vector of bytes that is stored on the heap. It is potentially mutable.
&str: an immutable borrowed string slice.
let s = "string"; // string literal (it's a slice)

Submodule rust_book/02/guessing_game added at da0a40d5d3