Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Skills:
ML Maths Basics80%
Key Takeaways
This video covers the basics of Rust programming language, including Vectors, Hash Maps, Casting, If-Let, While-Let, and the Result Enum, providing a comprehensive introduction to the language for beginners in the context of machine learning fundamentals.
Full Transcript
hi guys my name is tensor welcome to the introduction to Ross to toriel series this will be video 7 and before I get into what the topics are going to be for this video I just wanted to mention that the next video will be a project video the reason I'm going to do a project for the next video is because the channel just hit 1500 subscribers and I thought I should do something to celebrate it at least anyway in this video we're going to talk about vectors we're going to talk about hash maps we're also going to talk about two specific matched statements one called if let and the other one called if one and we're going to talk about testing and finally we're going to talk about the results ennum so we've looked at vectors before at least we've used them in some of the other tutorials so you can see here we're using this macro here to create a vector so what exactly are vectors well vectors are alike resizable arrays like slices their size is not known at compile time but they can grow and shrink at any time a vector is represented by three different pieces of data a pointer to the data its length and its capacity the capacity itself indicates how much memory is reserved for the vector and the vector can grow as long as the length is smaller than the capacity when this threshold needs to be surpassed the vector is then reallocated with a larger capacity and will actually take a look at that here in a moment so we can also create a vector using the effect new method as you can see here we're calling the namespace Becht and then we're saying new and this is a mutable vector this method here push allows us to put a new value into our vector V and then we're using a for loop to iterate through our reference to B and then print out each of the values now keep in mind that vectors can only have one type of value inside of them so running this program shows us that we have five six seven and eight inside of our vector we can also debug print out the vector itself so I'm calling a reference Tarab Ector here we can print out the length by calling vector dot length here and then we can print out the capacity as well by calling VD here's our vector and we have a length of four and a capacity of four if I take and push another value into our vector what will our capacity become as you can see here now our length is five and our capacity becomes eight so essentially what will happen is once we pass eight then it'll go up to sixteen and then once hoop has 16 it will go up to 32 and so on and so forth so we can also use a method called pop and this will pop the last value out of our vector you can see here that the result of that method actually gives us a option value so it says some 10 and we can destruct that to get the 10 out of it so to annotate vectors you put vector and then you put triangle brackets around the type annotation of the thing that you want to put inside your vector so in this case we want to put a 32 inside and because this is empty it should give us none instead of a sum we get back an empty vector with zero lengths and zero capacity and then we get back none as our answer for the pop method we can also embed an enum inside of a vector so despite a vector only allowing us to have one single type because an enum technically is a single type in this case our example enum example is a single type despite the fact that it has three different variations and as far as the vector is concerned these three different variations do are not enough of a difference to actually cause the vector to error out as you can see here we can print it out if we derive the debug we get our enum types back here which is int float and text each one with a different type inside of it so this one has a integer this one has a float and this one has a string inside of it so this type of behavior can be pretty useful for various different programs all right so now let's talk about hash Maps I have to make an import to gain access to the hash map this is because it's not technically part of the standard library at least not natively like vectors and arrays and so on and so forth the type hash map stores a mapping quis map to a value this does this be a hashing function which determines how it places these keys and values into memories many different programming languages of course have their own types of hash maps for instance python has dictionaries there are other names for it associative arrays as you can see from our example here we create a mutable hash map and then we can use the method insert to insert what we're doing is we're saying string comma and then an integer so our key will be of type string and then our value will be of type integer now like with vectors these need to be consistent types so for instance if I put a float in here instead of an integer we'll get an error and it'll say it mismatch types row we can also use this for loop here to iterate through our hash map our hash map has random 12 and then strings 49 inside of it now we can use this get function to gain access to an element inside of a hash map we're also going to use a match statement here because the actual return statement of this get method is an option so if we match on hm dot get and say we're looking for a string and we want to get the actual 12 outside of it then we need to destruct it by using the match statement and then we can just say okay well if it has a value connected to the key then return that value and print it out we get twelve out of the hash map we can also use a method called remove to actually remove not only the key but the value the key is associated with the value so if we just remove the key it will in fact remove the value as well and in fact I'm going to move this up above here and you'll see that we will only have one single field inside of our hash map you only have our random field in here if we call a key that doesn't exist inside our hash map as well it will return a none with the get method so in this case it would match with this and it would say no match that in fact does happen all right so consider this statement here we have a value S which has and then a character inside of it and if we want to get to that character we have to write an entire match Brock this can be a bit unwieldy that's why we have the if wet binding if we say it's wet and then we put some high so this is the actual branch itself here and then we set that equal to s so this is kind of like D structuring we're basically saying this pattern must equal this pattern where I is C and and then the sum part is the outer shell of that we have our actual result here so the code that we were on a run inside of here and then the other branch we can put inside of an optional else clause so this entire else clause can be removed if we want to you can see here that if we run it we get C back from both of these the one downside of using a if let binding is that it's not exhaustive like a match binding that is to say that we're only really looking at one case as opposed to all cases so it for instance we're trying to match on an enum that has like say 12 different fields and we're only looking at one field rather than all 12 of them so here's an even more verbose example we have a mutable s which has some 0 inside of it then we open up a loop and then we have another match statement on s in which we break down I and then we say if I is greater than 19 then we want to print out quit and turn s into none otherwise we want to print out the value of I and then we want to set s equal to hi plus 2 so in other words increment the value inside of the sum by 2 this doesn't match with some hi he just want to break outside of the loop here so when this gets set to none it'll just break outside of the loop and you can see here here's the actual behavior so it starts at 0 those 2 4 6 8 10 12 to 14 16 18 and then it says quit and breaks the loop rather than writing this and fairly verbose code here we can use what's called a wallet binding so as you can see here this is similar to our if let we're saying wallet and we're saying sum of I equals s then we have our if statements inside of this so basically we've cut this down to only a few lines of code as opposed to like 17 lines of code we're using destructuring here again basically pattern matching s with some I and this is only of course checking one case as opposed to multiple different cases and it's not exhaustive like our match statement here is so if I get rid of the loop and then I run this you'll see that it has the same exact behavior as before I go 0 2 4 6 8 etc all the way to quit and then breaks out of the loop these structures are syntactic sugar but they're pretty useful so Russ provides no implicit type conversion or coercion as it's called between primitive types instead there's what's called explosive conversion or a casting which can be performed with the as keyword as you see here so as you can see here we have a float bound to F and then we are casting this as a u8 into I essentially we're converting this from a float into a UA and then we're putting it into AI and then we're casting I as a care and putting it into C here as you can see here we get our float and then we get an integer and then we get this weird little character here so with converting from an integer to a character we need to use au AIDS so this means that our integer can only be from 0 to 255 so if we put 256 in here you can see literal out of range of u8 is the warning that we get but if we turn it into 255 it will in fact cast as a character and you can see here that we get this weird Y symbol that's used in european writing basically each of the u8 numbers correspond with a character some of my favorites are 12 and 14 12 gives us a female symbol and then 14 gives us this note symbol casting is pretty useful and we'll see it a bit more in the next tutorial and it'll give you a bit more context as to why it's fairly useful the actual character casting is something that you'd rarely use except in very very specific cases though so before we talked about the option enum now we're going to talk about an enum called result now this result enum is basically usually used for error-checking in rust you can also use the option enum 2 error check as well as we saw before you either get some and then a value or you get none with no value but the difference between option and result is that result will allow us to see why the function or whatever it is failed so rather than sending back some and then a value inside of it we get ok in a value inside of it and then if it fails we get eerr and a error inside of it and this is what the enum actually looks like now these are what I call generic types basically all this really means is that the type inside of ok can be different from the type inside of error now here's a pretty normal example of using the result enum so we want to read a filed I brought in this namespace here standard FS file we can say let F equal file open which opens this file this file needs to be of course in the root directory now we have this test dot txt file and we can say ok well match the file and see if we actually get the file in which case we just return the file otherwise we get an error and then we panic and what a panic does it will actually kick us out of the program in this case it will kick us out of the program with this text as well as the error that's included in the text so if I was to run this program right now because we actually have the file created here it will actually open and we'll actually see nothing but if I was to delete this file then we will for sure get this panic here here's what the panic looks like so says thread main panic tat there was a problem opening error and then it gives us an error code and a message so system cannot find the file specified we can get more information by running a rust back-trace which is something that we'll look at a little bit more when we look into error handling anyway I wanted to make you guys aware of the result enum because we will be using it in our project alright guys so if you enjoyed this tutorial feel free to subscribe and like if you have any questions or comments feel free to leave them in the comment box below and if you dislike this video then by all means download it as much as you'd like have a good evening guys
Original Description
In this video we talk about Vectors, Hashmaps, Casting, the If-let and While-let bindings as well as casting and the option enum. We fill in the gaps so that we can start to look at a full project.
Feel free to donate:
BTC: 1ExBSiaEa3pceW98eptJwzR9QHrYZ71Xit
ETH: 0xD210Ea51F1615794A16080A108d2BC5471F60166
Check out our Twitter: https://twitter.com/TensorProgram
Check out our Facebook: https://www.facebook.com/Tensor-Programming-1197847143611799/
Check out our Steemit: https://steemit.com/@tensor
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tensor Programming · Tensor Programming · 47 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
▶
48
49
50
51
52
53
54
55
56
57
58
59
60
NodeJs, Text editors and IDEs
Tensor Programming
Vanilla JS todo App
Tensor Programming
Elm Tutorial part 1
Tensor Programming
Elm Lang Tutorial, Part 2
Tensor Programming
Elm Tutorial Part 3
Tensor Programming
Elm Tutorial Part 4 -- Analog Clock App
Tensor Programming
Elm Tutorial part 5 -- Snake Game
Tensor Programming
Elm Tutorial part 6 -- Calculator
Tensor Programming
Go Tutorial part 1 -- Hello World and Static File Server
Tensor Programming
Go Tutorial part 2 -- Web Crawler
Tensor Programming
Go Tutorial Part 3 (Web App part 1)
Tensor Programming
Go tutorial Part 4 (Web tutorial part 2) - Using templates
Tensor Programming
Go tutorial part 5 (web app part 3)
Tensor Programming
Go tutorial part 6 (webapp part 4)
Tensor Programming
Go tutorial part 7 (web app part 5)
Tensor Programming
Go tutorial part 8 (Web app part 6)
Tensor Programming
Go tutorial Part 9 (web tutorial part 7)
Tensor Programming
Go tutorial Part 10 (web app part 8)
Tensor Programming
Go tutorial Part 11 (Web app Part 9)
Tensor Programming
Go Tutorial Part 12 (Web app Part 10)
Tensor Programming
Go Tutorial Part 13 (Web app Part 11)
Tensor Programming
Looking at Elm 0.18
Tensor Programming
Go tutorial Part 14 (Web tutorial part 12)
Tensor Programming
Go tutorial Part 15 (Web tutorial part 13)
Tensor Programming
Go tutorial part 16 (web app part 14)
Tensor Programming
Elm Tutorial Part 7 (SPA part 1)
Tensor Programming
Elm Tutorial Part 8 (SPA Part 2)
Tensor Programming
Electron Elm Tutorial
Tensor Programming
Go tutorial part 17 (web app part 15)
Tensor Programming
Up and Coming Programming Languages and Technologies for 2017
Tensor Programming
elixir tutorial part 1
Tensor Programming
elixir tutorial part 2
Tensor Programming
Elixir tutorial Part 3 (GenServer and Supervisor)
Tensor Programming
Elixir Tutorial Part 4 (GenStage)
Tensor Programming
Elixir Tutorial Part 5 (Plug and Cowboy)
Tensor Programming
Phoenix Framework Tutorial Part 1 (elixir part 6)
Tensor Programming
Phoenix Framework Tutorial Part 2 (elixir part 7)
Tensor Programming
Phoenix Framework Tutorial Part 3 (elixir part 8)
Tensor Programming
A Intro to Clojure and Clojure Syntax
Tensor Programming
An Update about the channel
Tensor Programming
Intro to Rustlang (Setup and Primitives)
Tensor Programming
Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Tensor Programming
Intro to Rustlang (Ownership and Borrowing)
Tensor Programming
Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Tensor Programming
Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Tensor Programming
Intro to RustLang (Enums and Options)
Tensor Programming
Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Tensor Programming
Rustlang Project: Snake Game
Tensor Programming
Intro to Rustlang (Traits and Generic Types)
Tensor Programming
Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Tensor Programming
Intro to Rust-lang (Modules and Lifetimes)
Tensor Programming
Intro to Rust-lang (Macros and Metaprogramming)
Tensor Programming
Intro to Rust-lang (Error Handling)
Tensor Programming
Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Tensor Programming
Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Tensor Programming
Rustlang Project: Port Sniffer CLI
Tensor Programming
Rustlang Project: Chat Application
Tensor Programming
Rustlang Project: CLI Toy Blockchain
Tensor Programming
Intro to Rust-lang (Setting up a Development Environment)
Tensor Programming
Intro to Rust-lang (Building a Web API with Iron)
Tensor Programming
More on: ML Maths Basics
View skill →Related Reads
📰
📰
📰
📰
Day 28 Part 1: No New Features Again — This Time We Make Everything Faster
Medium · Machine Learning
Evolving Algorithms: Next-Generation AI in Predictive Analytics
Dev.to · Fu'ad Husnan
Architecting for the Future: A Blueprint for Model-Agnostic, Business-Ready AI
Medium · AI
The Recommender System Pipeline: An End-to-End Overview
Medium · AI
🎓
Tutor Explanation
DeepCamp AI