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
-
String
should be used when its value should be passed around and operated on. Rust handles memory management forString
types, whereas when working withstr
, 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 thebreak
keyword. Second, during aloop
the rust compiler can detect variable initialisation (for variables declared outside of the loop). Forwhile
orfor
loops, the compiler here would fail. - Types in Rust often implement three kinds of iterators:
into_iter
,iter_mut
anditer
. 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 useiter
as this yields references to the values in aniterable
.iter_mut
should be used when we want to mutate those yielded values. Here is a great summary. -
vector
is toarray
is whatString
is tostr
! 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 theIndex trait
expects the types at indexi
andj
to be the same.tuple
types aretuple 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 amatch
arm - Does using
return
have any implications for functions? - What happens under the hood when you
impl
anenum
?
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)