30 Days of Rust - Day 3
Tags: rust, beginners
Published: 2023-04-25 21:02:42

What's up folks. Not going to lie, day 3 has left me tiiiiired. Drank a hot chocolate, so hoping to power thru here on a sugar rush. Much of what I covered today was just repeating ideas and syntax I'd covered in previous sessions, so my post is a bit shorter.

![sugar gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdj37ik6gptiwyb1h0y6.gif)

## Yesterday's questions answered
- `String` should be used when its value should be passed around and operated on. Rust handles memory management for `String` types, whereas when working with  `str` , the developer must take on that task. Generally, `str` is for reading, `String` for writing. Here's a  [good summary](https://stackoverflow.com/a/44407956/21306705).
- First, `loop` will [return a the result](https://stackoverflow.com/a/57382838/21306705) of an expression which follows the `break` keyword. Second, during a `loop` the rust compiler can [detect variable initialisation](https://stackoverflow.com/a/28892433/21306705) (for variables declared outside of the loop). For `while` or `for` loops, the compiler here would fail.
- Types in Rust often implement three kinds of iterators: `into_iter`, `iter_mut` and `iter`. For loops implicitly call `into_iter`. This method moves the `iterable`, meaning once all values have been yielded, you can no longer access the originally referenced value. Generally, we should use `iter`  as this yields references to the values in an `iterable`. `iter_mut` should be used when we want to mutate those yielded values. Here is a [great summary](https://hermanradtke.com/2015/06/22/effectively-using-iterators-in-rust.html/).
- `vector` is to `array` is what `String` is to `str`! A [nice summary](https://www.cs.brandeis.edu/~cs146a/rust/doc-02-21-2015/book/arrays-vectors-and-slices.html). Basically, vectors are suited for data manipulation, whereas arrays are better suited to "read-only" situations.
- `tuple` is not indexed in the same way as other collections, as the `Index trait` expects the types at index `i` and `j` to be the same. `tuple` types are `tuple indexed` and are ordered. [Official docs](https://doc.rust-lang.org/std/primitive.tuple.html).
- Generally, the `target` folder should not be checked in. If you're building an executable, `Cargo.lock` should be checked in. For libraries it should not. Here's the [source](https://stackoverflow.com/a/41275118/21306705).

## Today's open questions
- Can I call functions in the left side of a `match` arm?
- What does `Some` mean in the context of a `match` arm
- Does using `return` have any implications for functions?
- What happens under the hood when you `impl` an `enum`?


## Functions don't return stuff???
![dafuk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syp8x6x78n2o16vo6gfa.gif)

That heading is slightly misleading: functions in Rust do indeed return stuff, but functions do not require the `return` keyword. Instead, you can return a value by referencing it in an expression that omits `;` (which would otherwise be required). This is very new to me and I want to know if these omissions have any implications.
```rust
fn return_true() -> bool {
	true
}
```

Functions in Rust can also return multiple value (of different types). I didn't go into much depth today, I'm sure there are more oddities surrounding functions in Rust.

## Enum(bs)
![terrible pun](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r5bkl84i0n8cf9p8oeb9.jpeg)

Enums provide a programmatic way of grouping values of limited possibilities in one structure. For example the enum `Day` may contain a value for each day of the week. In Rust enum values are declared in a comma-delineated list enclosed in curly braces.

You can access values of an enum using the `::` operator. Also the integer operand `|` works for enums as they are interpreted as integers.

## Castaway (me after a few more days)
![Tom hanks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42e7ex6fb58v9qom7mgz.jpeg)

TypeScript folk may know the `as` keyword to give type aliases. In Rust `as` is more powerful and can be used to cast one type `as` another.