[01×12] How to use Arrays in Rust
Video Title: [01×12] How to use Arrays in Rust
Welcome to doggo.rs, your home for wholesome Rust tutorials. [swoosh] So far, you’ve seen several different data types, including integers, floating-point numbers, characters and strings. While these are useful on their own as individual pieces of data, at some point, you may want to organize your data into a collection,
So that your data is easier to manage. In the coding world, a collection of data types is called a “data structure”. To add to the confusion, a data structure is also a data type. Different programming languages use different naming conventions for their data structures, so it’s important to understand how these
Data structures work in these languages, as opposed to getting hung up with their names. In Rust, data structures are called “compound types”. An example of a Rust compound type is an Array. In Rust, an Array is a primitive compound type. The elements of a Rust Array must be of the same data type.
And once declared, the length of the Array cannot be changed. Let’s take a look at some examples. I’m in VS Code, where I’ve already created a new .rs file. I’ve also written the boilerplate code for the main() Function. To construct an Array, use a pair of square brackets.
In between these square brackets, enter your data, separated by commas. The individual pieces of data are called “elements”. To display the Array, you have a couple of options. One option is to use “colon-question mark” in between the curly brackets. This option will display the Array in a single row.
The other option is to use “colon-hashtag- question mark” in between the curly brackets. This option will display the Array in a single column. [error sound] So the Rust Compiler is trying to be helpful here by warning you that the variable doesn’t need to be mutable. While that may be true at the moment,
We’re going to mutate this Array in a few minutes, so you can disregard this warning message. You can see the output a little further down, after a warning message. So in this example, you declared a mutable variable named “array”, and the value assigned to it is a collection of 32-bit integers
In the form of a Rust Array. Here, “mutable” means that you can mutate the value of the individual elements, as long as the new value is of the same data type as the other elements. In addition, you can sort a mutable Array. But you can’t mutate the length of a Rust
Array, whether it’s mutable or not. To access an individual element in an Array, use square brackets immediately following the variable name. Within the square brackets, include the index number. In the coding world, depending on the programming language, indexing is either 0-based or 1-based. 0-based indexing means that the initial
Element is index 0, and the second element is index 1, and so on. 1-based indexing means that the initial element is index 1, and the second element is index 2, and so on. Rust uses 0-based indexing, so the initial element in a Rust Compound type is index 0.
Disregard the warning message again and view the output after the warning. In this example, you’re accessing the second element in this Array, which is index 1. To mutate the value of an element, use the variable name, along with the index number. Note that the warning message went away since you mutated the variable.
In this example, you mutated the value contained at index 1, which is the second element, from 2 to 20. To find the length of an Array, use the “.len()” method. While this may not be that useful for this simple example, this method comes in handy for very large Arrays.
To add the value of all of the elements in an Array, use the “.iter()” method, along with the “.sum()” method. When declaring the variable that will be assigned the total value, you also need to declare the Data Type of the results. Again, this may not be that useful for this simple example,
But it’s extremely useful for large Arrays. To sort an Array, Rust offers some options, but they may not behave like you might expect them to. The “.reverse()” method, sorts the elements of the Array in reverse order by index number and not by value. In the output, the original first element
Is now the last element, and the original last element is now the first element. This may, or may not be what you were looking for. The .sort() method, sorts the elements of the Array in ascending order by value. In the output, the Array is now sorted in ascending order by value.
Now that you have the Array sorted in ascending value order, you can use the “.reverse()” method to sort the Array in descending order. In the output, the Array is now sorted in descending order by value. There’s another way of sorting in descending value order in Rust that doesn’t require two
Steps, but the code for this alternate way is a bit more advanced, so I won’t be covering it in this beginner series. In Rust, an Array is a single dimension, so you can’t construct a two-dimensional Array with rows and columns. But you can construct an Array of Arrays, meaning an Array
Where each element is also an Array. In this example, element 1, or index 0, of the Array of Arrays is the first Array and element 2, or index 1, of the Array of Arrays is the second Array. To access individual elements, use the index number for the overall Array in square brackets,
Immediately followed by another pair of square brackets for the index number of the specific Array. Since indexing in Rust is zero-based, the first Array is index zero, and the second element in that Array is index one. Also, the second Array is index one, and the third element in that Array is index two.
If you’re new to programming, zero-based indexing may be a little confusing, so be sure to practice by writing your own code until you’re comfortable with how this works. So that’s the basics of using Arrays in Rust. In the next chapter, you’ll learn what a Vector is and how to use it in Rust.
[swoosh]
Video Keywords: Rust Programming, [vid_tags]
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
10:40 Speaking of rows and columns, you may have heard of the "Polars" library in Rust (not the python version, the version that you can use with Rust) :
(Can't seem to post the link unfortunately).
Not as easy to start with compared to DataFrames.jl, but it's fun to have a crack at.
Great video btw. Learnt a few extra bits about Arrays in Rust. Love your Julia vids too !