rust/notes

234 lines
5.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)