Files
rust/notes
2022-12-16 10:00:30 -05:00

141 lines
2.8 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