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.
Yesterday's questions answered
-
Stringshould be used when its value should be passed around and operated on. Rust handles memory management forStringtypes, whereas when working withstr, the developer must take on that task. Generally,stris for reading,Stringfor writing. Here's a good summary. - First,
loopwill return a the result of an expression which follows thebreakkeyword. Second, during aloopthe rust compiler can detect variable initialisation (for variables declared outside of the loop). Forwhileorforloops, the compiler here would fail. - Types in Rust often implement three kinds of iterators:
into_iter,iter_mutanditer. For loops implicitly callinto_iter. This method moves theiterable, meaning once all values have been yielded, you can no longer access the originally referenced value. Generally, we should useiteras this yields references to the values in aniterable.iter_mutshould be used when we want to mutate those yielded values. Here is a great summary. -
vectoris toarrayis whatStringis tostr! A nice summary. Basically, vectors are suited for data manipulation, whereas arrays are better suited to "read-only" situations. -
tupleis not indexed in the same way as other collections, as theIndex traitexpects the types at indexiandjto be the same.tupletypes aretuple indexedand are ordered. Official docs. - Generally, the
targetfolder should not be checked in. If you're building an executable,Cargo.lockshould 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
matcharm? - What does
Somemean in the context of amatcharm - Does using
returnhave any implications for functions? - What happens under the hood when you
implanenum?
Functions don't return stuff???
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
}
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)
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)
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)