Today's session was short but packed full of data types and looooops 🔁
::operator is used to access associated functions and associated constants from
mods. It can also be used to access constants, functions or static methods from structs or enums.
.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.
matchexpressions are the only context where
=>is valid Rust
- The only other kind of limitations for the right side of a
match armis that the code is an expression.
macrosare functions that write Rust code. They can take a variable number of parameters. They can implement traits because
macrosare expanded at compile time.
- Type annotations aren't strictly required if there is implicit type.
- What are common patterns for using
- When would you use a
- What data types require
vectortypes have other advantages over arrays other than extendability?
tupletypes technically indexed? Or why can't I slice a
- Do I keep my target folder in version control (probably not)?
Go, Rust has a few different ways to loop. These are outlined below.
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.
The near omnipresent
while loop also features in Rust. Nothing much to say here...
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.
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.
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.
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.
Strings in rust can be easily converted between characters and bytes. Single characters wrapped in single quotes are considered characters rather than strings.
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.
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_str methods on a
String to add new chars or strings to it. Alternatively, you can use the