Compare commits
15 Commits
d1a3ea1a96
...
main
Author | SHA1 | Date | |
---|---|---|---|
7d92f735c9
|
|||
b8f94f79aa
|
|||
aea4ade81b
|
|||
804501654b
|
|||
8f32bb41ba
|
|||
9bca80091f
|
|||
da9b8386e9
|
|||
1f1ea6de94
|
|||
9a7a9558b5
|
|||
6db7d9edf7
|
|||
055e0d69ae
|
|||
ddd68240e7
|
|||
7cbfe8dbc9
|
|||
915ba1a499
|
|||
590c683d98
|
7
guessing_game/Cargo.lock
generated
7
guessing_game/Cargo.lock
generated
@@ -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"
|
|
@@ -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]
|
|
@@ -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 that’s 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}");
|
|
||||||
}
|
|
@@ -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":{}}
|
|
@@ -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/
|
|
@@ -1 +0,0 @@
|
|||||||
96b64cf4f2c856d9
|
|
@@ -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}
|
|
Binary file not shown.
@@ -1 +0,0 @@
|
|||||||
This file has an mtime of when this was started.
|
|
Binary file not shown.
@@ -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:
|
|
Binary file not shown.
@@ -1 +0,0 @@
|
|||||||
/home/luket/git/rust/guessing_game/target/debug/guessing_game: /home/luket/git/rust/guessing_game/src/main.rs
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2
hello_cargo/.gitignore
vendored
2
hello_cargo/.gitignore
vendored
@@ -1,2 +0,0 @@
|
|||||||
Cargo.lock
|
|
||||||
target
|
|
@@ -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]
|
|
@@ -1,3 +0,0 @@
|
|||||||
fn main() {
|
|
||||||
println!("ǰ DZ Dz dz Ǵ ǵ Ƕ Ƿ Ǹ ǹ Ǻ ǻ Ǽ ǽ Ǿ");
|
|
||||||
}
|
|
233
notes
Normal file
233
notes
Normal 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 value’s 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;
|
||||||
|
|
||||||
|
Rust’s 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)
|
1
rust_book/02/guessing_game
Submodule
1
rust_book/02/guessing_game
Submodule
Submodule rust_book/02/guessing_game added at da0a40d5d3
Reference in New Issue
Block a user