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
43 changed files with 234 additions and 67 deletions

View File

@@ -1,7 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "guessing_game"
version = "0.1.0"

View File

@@ -1,8 +0,0 @@
[package]
name = "guessing_game"
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,26 +0,0 @@
// some things are automatically loaded in every rust program
// https://doc.rust-lang.org/std/prelude/index.html
// for extras, import them with `use`:
use std::io;
fn main() {
println!("Guess the number!");
println!("input your guess");
// vars are immutable by default, change this
// The :: syntax in the ::new line indicates that new is an _associated function_ of the String
// a function thats implemented on a type
let mut guess = String::new();
// could still call this without importing it via std::io::stdin
// this can be one line, it is broken up for readability.
io::stdin()
// & indicates that this argument is a reference,
// references are also immutable by default so &mut is needed
.read_line(&mut guess)
// expect handles the Err variant by crashing the program. There are other ways to handle
// errors.
.expect("Failed to read line");
// read_line returns a `result` value. result is an enum. Each possible state is a variant.
// Result's variants are Ok and Err
println!("you guessed: {guess}");
}

View File

@@ -1 +0,0 @@
{"rustc_fingerprint":2873261240282726551,"outputs":{"10376369925670944939":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/luket/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu\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=\"llvm14-builtins-abi\"\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_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=\"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=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_thread_local\ntarget_vendor=\"unknown\"\nunix\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.67.0-nightly (7eef946fc 2022-11-06)\nbinary: rustc\ncommit-hash: 7eef946fc0e0eff40e588eab77b09b287accbec3\ncommit-date: 2022-11-06\nhost: x86_64-unknown-linux-gnu\nrelease: 1.67.0-nightly\nLLVM version: 15.0.4\n","stderr":""},"15697416045686424142":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n","stderr":""}},"successes":{}}

View File

@@ -1,3 +0,0 @@
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by cargo.
# For information about cache directory tags see https://bford.info/cachedir/

View File

@@ -1 +0,0 @@
{"rustc":15234846723159686366,"features":"[]","target":18246892274002150725,"profile":9251013656241001069,"path":1684066648322511884,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/guessing_game-3f658c19dd973748/dep-bin-guessing_game"}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}

View File

@@ -1 +0,0 @@
This file has an mtime of when this was started.

View File

@@ -1,5 +0,0 @@
/home/luket/git/rust/guessing_game/target/debug/deps/guessing_game-3f658c19dd973748: src/main.rs
/home/luket/git/rust/guessing_game/target/debug/deps/guessing_game-3f658c19dd973748.d: src/main.rs
src/main.rs:

View File

@@ -1 +0,0 @@
/home/luket/git/rust/guessing_game/target/debug/guessing_game: /home/luket/git/rust/guessing_game/src/main.rs

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