30 days of Rust – Day 2

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 consta…


This content originally appeared on DEV Community and was authored by johnnylarner

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

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

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.


This content originally appeared on DEV Community and was authored by johnnylarner


Print Share Comment Cite Upload Translate Updates
APA

johnnylarner | Sciencx (2023-04-24T19:40:01+00:00) 30 days of Rust – Day 2. Retrieved from https://www.scien.cx/2023/04/24/30-days-of-rust-day-2/

MLA
" » 30 days of Rust – Day 2." johnnylarner | Sciencx - Monday April 24, 2023, https://www.scien.cx/2023/04/24/30-days-of-rust-day-2/
HARVARD
johnnylarner | Sciencx Monday April 24, 2023 » 30 days of Rust – Day 2., viewed ,<https://www.scien.cx/2023/04/24/30-days-of-rust-day-2/>
VANCOUVER
johnnylarner | Sciencx - » 30 days of Rust – Day 2. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/04/24/30-days-of-rust-day-2/
CHICAGO
" » 30 days of Rust – Day 2." johnnylarner | Sciencx - Accessed . https://www.scien.cx/2023/04/24/30-days-of-rust-day-2/
IEEE
" » 30 days of Rust – Day 2." johnnylarner | Sciencx [Online]. Available: https://www.scien.cx/2023/04/24/30-days-of-rust-day-2/. [Accessed: ]
rf:citation
» 30 days of Rust – Day 2 | johnnylarner | Sciencx | https://www.scien.cx/2023/04/24/30-days-of-rust-day-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.