30 days of Rust - Day 2
Tags: rust, beginners, community
Published: 2023-04-24 19:40:01

Today's session was short but packed full of data types and looooops 🔁

## Yesterday's questions answered
- The `::` operator is used to access *associated functions* and *associated constants* from `crates` or `mods`. It can also be used to access *constants*, *functions* or *static methods* from structs or enums.
- The `.` operator is used to access fields and methods of a struct or enum. These methods cannot be static methods.
- Double character operators are logical operators whereas single character operates are bitwise operators and work only with integer operands.
- `match` expressions are the only context where `=>` is valid Rust
- The only other kind of limitations for the right side of a `match arm` is that the code is an expression.
- `macros` are functions that write Rust code. They can take a variable number of parameters. They can implement traits because `macros` are expanded at compile time.
- Type annotations aren't strictly required if there is implicit type.


## Today's open questions
- What are common patterns for using `String` vs. `&str`?
- When would you use a `loop` instead of `for` or `while`?
- What data types require `.iter()` in a `for` loop?
- Do `vector` types have other advantages over arrays other than extendability?
- Are `tuple` types technically indexed? Or why can't I slice a `tuple`?
- Do I keep my target folder in version control (probably not)?


## Rust got me loopy
![loops](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9u1r474aqvw79rgn2et0.jpg)

Unlike `Go`, Rust has a few different ways to loop. These are outlined below.

### For loop
Python programmers like me can code `for` loops by muscle memory. So there wasn't too much new here to learn. The only major difference is that some data types seem to require you to explicitly call an `iter()` method, while others don't. I'm going to follow up on this in my next post.

### While loop
The near omnipresent `while` loop also features in Rust. Nothing much to say here...

### Loop loop
Rust also offers a `loop` loop. A `loop` offers no boolean check per iteration like `while` loop. Nor does it offer the value provided by an object's `iter()` method as in a `for` loop. So far I've only seen rudimentary usage using an index counter. I'll explore its uses more in the next post.


## Collection basics
### Array
No surprises here, but Rust does indeed support Arrays, which are ordered and have a fixed size. Elements of an array must be of the same type, and we can slice arrays using square brackets.

### Vectors
The vector type is similar to an array, only that it is extendable. They support index slicing like arrays and have additional methods like `dedup()` which removes duplicates. I'm not sure what the nuances of this data type are - I'll shed more light tomorrow.

### Tuples
No other programming language that I've studied (which isn't many) has supported tuples. Tuples are arrays of fixed sized that can contain elements of different types. Tuples don't support index slicing, but instead can use dot notation to access elements. I want to understand why this is the case, so I'll put that into tomorrow's article.


## String city
![city of strings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vejfola04mzo4o7yxktk.png)

Strings in rust can be easily converted between characters and bytes. Single characters wrapped in single quotes are considered characters rather than strings.

### &str
Literal strings in Rust are known as string slices. I presume this is a lower-level implementation and need only be used in cases where performance optimisations are important.

### String
The more common string type is `String`, which is a struct that implements static methods to yield `String` types from string literals. You can call the `push` or `push_str` methods on a `String` to add new chars or strings to it. Alternatively, you can use the `+`
operator.