Skip to content

Instantly share code, notes, and snippets.

@moozzyk
Last active December 31, 2018 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moozzyk/7bbb6d5aff2c7239e8c53bad454866ab to your computer and use it in GitHub Desktop.
Save moozzyk/7bbb6d5aff2c7239e8c53bad454866ab to your computer and use it in GitHub Desktop.
Good documentation/starting guide
Should have read more before starting (e.g. the borrowing concept)
unwrap().unwrap().unwrap() everywhere (likely I am doing something wrong)
iter() vs into_iter() confusing for beginners
regex - quite easy to get it up and running
when is ';' (not) required?
lack of traditional `for` loop - counter needs to be in the loop
pattern matching
lifetimes/borrows are killing me - day 4 - could not return HashMap from a function
hint in errors are helpful:
```
expected struct `std::vec::Vec`, found &[u8]
help: try using a conversion method: `line.as_bytes().to_vec()`
```
str vs. String
e.g. let s = "dabAcCaCBAcCcaDA".to_string(); looks weird
----
let mut root = "";
if !visited.contains(c) {
| ^^^^^^^^ the trait `std::borrow::Borrow<std::string::String>` is not implemented for `&&str`
https://github.com/rust-lang/rust/issues/43940
Fixed by let mut root = String::from("");
Doubly linked list is apprently a problem (link to blogs)
day10
- you can access tuple item by .0, .1 without having to destructure
day14
why do I need borrow 0?
--> src/main.rs:143:68
|
143 | if is_in_range(map, &creatures[idx]) || &creatures[idx].hits < 0 {
| ^
| |
| expected &i32, found integral variable
| help: consider borrowing here: `&0`
|
= note: expected type `&i32`
found type `{integer}`
day17
Allocating 2d array
let mut geo_map = [['.'; 1000]; 1000];
works
let mut geo_map = [['.'; 2000]; 2000];
thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Abort trap: 6
day18
usize for indexing can be annoying - e.g. when processing neighbours in an array. Casting to and fro is gross. Ended up with:
```
for row_i in if row == 0 {0} else {row-1}..=if row < map.len() - 1 { row + 1 } else { map.len() - 1 } {
for col_i in if col == 0 {0} else {col - 1}..=if col < map[row].len() - 1 {col + 1} else {map[row].len() - 1} {
```
- no global variables
- how many times I typed println instead of println!
- !vec vs vec!
- searching for rust Z3 is tough (day 23)
- no function overload
- local functions (day23), as opposed to lambdas
- cannot get convinced to the short return version (i.e. no `return` and no `;` at the end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment