From the course: Rust for Data Engineering

Introducing Rust sequences and maps - Rust Tutorial

From the course: Rust for Data Engineering

Introducing Rust sequences and maps

- [Instructor] Here we have Rust Collections, and a few important things about Rust Collections, is there are some similarities to Python. For example, with a sequence it's just like a Python list, more or less and with a map it's just like a Python dictionary. But one of the key differences is that they're immutable by default. So, let's go ahead and take a look at this sequence here. At the very beginning, notice that the let fruits equals vec apple banana cherry. This creates an immutable list. So, you can't change it in the future, and this is a safety feature of Rust. The output, if you printed it would be apple banana cherry. Likewise with maps, if you go ahead and you try to insert something into a default hash map and you don't create a mutable value. It won't work because Rust is going to protect you from creating a mutable structure unless you explicitly say mute. Now, if we go ahead and take a look at what the output of something would be. You can see it's going to be a key value pair. Apple ten, banana eight, orange fifteen. So, in a nutshell here, similar to Rust, Python also uses lists as sequences, and dictionaries, and Python lists are dynamic arrays, and they're similar to a Rust vector and with a Python dictionary, like Maps and Rust, they're key value stores in terms of access patterns in both Python and Rust. The elements in a list, Python or sequence and Rust are accessed by their position. Duplicate values, a Python list does allow duplicate values just like a Rust sequence, but a dictionary both in Python or Rust Map don't allow you to have duplicate keys. Likewise, one of the key differences is iteration order as well is going to be different because in Python, by default a dictionary does actually, in the new versions maintain the order, but it wouldn't in Rust. Finally, one major difference here is type safety and this is a key reason for using Rust is that Python's listing dictionaries are dynamically typed meaning you could put anything aside but Rust will have static typing, which means that it only can contain things that explicitly are typed. And this is a great security feature of Rust.

Contents