Check out a free preview of the full Polyglot Programming: TypeScript, Go, & Rust course

The "Fissures Rust Setup" Lesson is part of the full, Polyglot Programming: TypeScript, Go, & Rust course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen walks through installing Cargo, a Rust package manager, and the anyhow error type.

Preview
Close

Transcript from the "Fissures Rust Setup" Lesson

[00:00:00]
>> All right, we'll go to rust, we'll go to projects. Now, there's a problem here. This is my old main file, this is the only entry into our binary. So to allow multiple entries, we create a folder called bin and then we have to move main into that bin.

[00:00:13]
So I'm gonna move main and I'm just gonna call p1.rs. So now when I run it, instead of doing cargo run, I'm gonna go bin p1 and it will run that one, right? Nice way to be able to have multiple entries into your program, so src/bin. So there we go, we're in src/bin, you can see that right down here, if you don't know how to see those things src/bin down here, I have one file.

[00:00:36]
Let's create a p2.rs. And we're gonna do the exact same thing. I'm gonna hop back to my previous project, and I'm going to grab this thing, put it in my clipboard, paste it in. Function goes to fn, colon goes to dash over, string goes of course to ampersand, that, static cuz it lives forever, do that.

[00:00:55]
Grave, as we've all learned, goes to here, go down, and of course grave right here, does that. There we go. We now have this beautiful piece of getting input and it's still complaining because we forgot to rename this as get_input. It will always complain until you have a main function, so don't forget to get a main function in there.

[00:01:17]
There we go. So now let's create our structs, right? So do the exact same thing, we're gonna do struct point, we'll have x as an i32, a y as an i32. We will have a line as a p1 and a p2. And that will be a point and a point, there we go, right.

[00:01:38]
We've done this a couple times now, we're pretty much familiar. You could have copied and pasted the TypeScript code, removed the equal sign, and it almost have been exactly how it should be. So it's pretty straightforward. Now, this is where we're gonna do something that I think is pretty cool.

[00:01:52]
We're gonna implement a trait, a trait, remember, is just an interface. If you implement interfaces, you can actually interact with a lot of the standard functions that are available inside of Rust. So we're gonna do FromString, now this is a beautiful, beautiful thing. So we're wanna go FromString and we're gonna pass in a point.

[00:02:10]
Let's see. Four point, I believe that is. So there we go. Now we have this, so it's wanna give us this error, not everything's implemented, just like any interface, you have to implement all the items. So you can use code actions to give you the bare bones kind of implementation.

[00:02:31]
So we were on using our first trait. This is something we haven't seen yet. Traits are fantastic. Like I said, they are just like interfaces from, say, TypeScript. If you do an interface foo and then you allow a class to implement it, you have to add those methods to it.

[00:02:46]
It's backwards in Rust in the sense that you create your struct like I did right here for, say, point. And then I say, endpoint also implements this interface or this trait. And by implementing this trait, there's other functions that just take in trait objects or they're generic on trait objects and they're able to do operations based on these trait methods.

[00:03:09]
And so that is actually pretty cool. It allows some pretty radical stuff to be done. So let's just get started right here. So, make sure you do a cargo install cargo-edit. This gives you the ability to do a cargo add and you can just simply add libraries to your cargo.toml.

[00:03:24]
You can hand edit your package.json in TypeScript. You can hand edit your cargo.toml in Rust. Should you do it? Well, I mean, if something already does it for you, gets the correct version, all that, why would you ever want to do it, correct? That's at least how I personally feel.

[00:03:40]
So just install this thing, it might take a moment and then afterwards you'll be able to do what I did, which of course is cargo add anyhow. Anyhow is the error handler that we did talk about earlier, it's pretty darn good at handling errors. And since we're now starting to get into the more error prone aspects of Rust, it's good to have this.

[00:04:00]
I also made a distinction earlier, this error, which is another error handling library, is really great if you're writing a library that someone else will consume. Anyhow is if you're writing an application that you want to execute, anyhow kind of, it's just a little different. This error produces you concrete types of errors in which you can have certain responses and certain values to be produced back out, which makes it very nice for a library, because then there's an expected error type.

[00:04:26]
Everything's great, and it's not a this error, it's like the error you've created. And so it's just a little bit nicer, of course, it uses enums, all the fun stuff, but we're gonna be using anyhow. I strongly recommend checking out this error if you're building a library

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now