DEV Community

Cover image for 30 Days of Rust - Day 3
johnnylarner
johnnylarner

Posted on • Edited on

30 Days of Rust - Day 3

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

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.
  • First, loop will return a the result of an expression which follows the break keyword. Second, during a loop the rust compiler can detect variable initialisation (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.
  • vector is to array is what String is to str! A nice summary. 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.
  • 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.

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

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.

fn return_true() -> bool {
    true
}
Enter fullscreen mode Exit fullscreen mode

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

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

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.

Top comments (0)