- Podcasts
- ADSP: Algorithms + Data Structures = Programs
- Episode 101: C++ Developers Try Rust!
Episode 101: C++ Developers Try Rust!
ADSP: Algorithms + Data Structures = Programs
- October 28, 2022
- 23 min
About this episodeIn this episode, Bryce and Conor live code some Rust! Link to Episode 101 on Website Twitter ADSP: The Podcast Conor Hoekstra Bryce…
In this episode, Bryce and Conor live code some Rust!
Link to Episode 101 on Website
Show Notes
Date Recorded: 2022-10-27
Date Released: 2022-10-28
- CityStrides.com
- Rust Podcast Twitter Poll
- Rust Programming Language
- C++ std::vector::front
- Rust std::iter::IntoIterator::into_iter
- Rust std::option::Option::unwrap
- Rust std::iter::Iterator::next
- Accessing First Element of Vec in Rust (Godbolt)
- JT on Twitter
- Mara Bos on Twitter
- Jakt Programming Language
- Refactor of Rust vec[0] Commit
Intro Song Info
Miss You by Sarah Jansen https://soundcloud.com/sarahjansenmusic
Creative Commons — Attribution 3.0 Unported — CC BY 3.0
Free Download / Stream: http://bit.ly/l-miss-you
Music promoted by Audio Library https://youtu.be/iYYxnasvfx8
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Episode 101: C++ Developers Try Rust!
And the compiler is hugging me and basically holding my hand as I learn the language. I feel like Rust the Rust compiler hugs you while you're learning, and like the C compiler actively like punches you in the face. I hope you're still recording because that was uh welcome to ADSP the podcast episode 101, recorded on October 27th, 2022. My name is Connor, and today with my co-host Bryce, we live code some rust. We might do a little live coding. We will we we said we'd never do this again, but um I might need to join from my other laptop. But first, first things, well actually let me first join. This is gonna be exciting, folks. We're gonna start off with a a Twitter poll. We haven't done a Twitter poll, I feel like, in months.
Um no, no, it actually has nothing uh nothing to do with us personally. But first things first, let me go to I gotta go to Oh, while I'm doing this, let me explain to you. I'll just this is completely random. Our listeners don't care about this, but there's a website called City Strides, and it's basically keeps track of every road you run on if you sync it with your Strava or Garmin. And I've recently become, and then it it like ranks you out of all the people on the website of how many roads you run globally. And I have recently become addicted to this. Anyways, uh it's pretty neat. So for those of you that are running or walking and want to see all the streets you've ever walked on, go check out CityStrides.com. Alright, let me share my screen. First, we're gonna set a Twitter poll. And I guess actually we need to do this on Firefox because that's where desktop one. Share. Thanks for hanging in there, dear listener. All right, let's open up Firefox now. And we are going to construct a tweet. And the tweet is. I'll type it live actually, so Bryce will keep Bryce on the edge of his seat. Ooh, we got some notifications first.
So um there's like four other podcasts I want to start. And one of them is a Rust podcast. But we could I could start a Rust podcast, or ADSP could just briefly briefly become a Rump. Yeah, alright, so that's but that's what the poll is about. But apparently each choice, each option is only allowed 25 characters.
What do um listeners prefer? A ADSP becomes we'll say temporarily temporarily temp, but I can't spell temp orarily becomes a at Rustlang podcast or B at code report starts a new Rustlang only podcast. Uh and then we put A and B. And we'll put this for a week. Does that look good? Not even paying attention, it's just so focused on his chocolate.
It's sent. Alright, now I'm gonna take my hoodie off to become more comfortable for this live coding thing. One sec. Alright, let's talk about. Should we ask a question first? Let's ask a question. Um how do we form this question? How many functions do you think you need to call to idiomatically, and when I say idiomatically, I mean idiomatically from the point of view from a person that doesn't know Rust and just started trying to pick up Rust like a few days ago, uh, access the first element of a vector in Rust. So the what is the equivalent um of vec.front, or like, you know, if v is your vector and you go dot one pardon?
I think technically there is a literal array um equivalent to the std array, but this is like you know, for for dealing with uh uh a vector equivalent to the std vector in C. This is what you would use. So you can see here at the bottom we got one, two, three. So my understanding is that uh if we want to get the first element, we have to go V either into or into iter, and then we have to go next, and then we have to go unwrap.
And so like next, I believe, returns basically it's the equivalent of your like plus plus um operator um in C, but it returns an option of the next element because you might have an empty list. So that's similar to like if you're familiar with functional languages where they have head and tail where head is equivalent to the first element. They also have like safe head, which returns you a maybe of the underlying type of your vector or list. And so because next is returning an option, you need to unwrap it. And I'm sure there's a better way to do it than unwrap. But anyway, so the point is that in order to get the first element of a vector, you have to go v or whatever the name of your vector is, dot into iter, parenpren, dot next, parenpren, dot unwrap, parenpren.
Okay, so so why there might there must like I'm sure we're not the first people to um come across this. There must be a principled reason why actually doesn't do this. First, uh Can we can we can we get isn't isn't Mara like the the Rust standard library person? Can we can we get her on the podcast? I don't know if you're not.
I know Jane, uh Jane is super involved and we know her. We can get Jane on. Also, too, we should definitely have on um JT, uh Jason's cousin, because he works both on Rust and on Yacht, or Yakt or Jacked, or whatever the language that's in Serenity OS. So we can ask about two languages at once. Um does Rust have bracket?
I okay, it doesn't have to be brackets. It could be a function called like, you know, at, which is a name of a much yes, much less useful function in on vector in C. An at could return you a Rust maybe. It could take like an index and return you a Rust maybe. Like why doesn't why doesn't Rust have this?
Shame on you, Bryce. Shame on uh you like basically trying to make a beginner developer that's learning about a technology not feel like feel bad about themselves. This is why people don't want to get up on stage at conferences and give talks and stuff, because like you're trying to make oh, you want you want people to know that you didn't know? Of course I didn't know. I'm learning the language. And Stack Overflow didn't tell me that this was possible. But let's let's go and check if this actually I I appreciate your efforts to save this episode. Man, it's not nice, buddy. Not nice. So let's go check if this actually works. So yeah, don't save. Go back here. Alright, so now we're in live code and I'm just changing where I had an into iter next unwrap. Changing this to angle bracket zero. And now let us try and build and see what happens. Move occurs because the value of type T, which does not implement the copy trait, helps. Uh consider borrowing here. Alright, we will do that. Also, how wonderful. I mean, Bryce has left his computer, so we're not really live coding anymore.
And it is funny like how often this happens where like you um you think you've discovered some problem or something, and um and then you go to show this to somebody else, and then as you're showing it to somebody else, it's like the the it it becomes immediately clear that oh no, it wasn't a problem, you just there was just confusion or misunderstanding or something. And I think merely the act of uh of explaining something to other people.
You probably were looking for like a dot front or something. Um and I think that's because of like this is again a good example of the difference in type of programmer that we are. Um my gut reaction was to reach for angle bracket zero if I want like the first element of a thing. Because to me, from more of the systems programmer background system programming background background, it's just like the first element in a in a you know a contiguous array of memory. Um or you know, really it's more the the pointer syntax of which I am uh used to working with uh as somebody who writes a lot of low-level code. Whereas you, I think the first thing that you would reach for would be something like dot front, and you would probably think that something like open bracket uh zero close bracket is in some way inelegant. And so maybe you didn't try that. Maybe you tried dot front and you never tried this.
That is probably some good, what do you call it, reverse historianing in that probably that is what I did is I looked for front, didn't find front, found next, but next didn't work. And in order to use next, you had to call into iter, but that only gave that gave you an optional, and so in order to get the value out of the optional, you had to go unwrap, and then I said, wow, this is pretty awful. I never thought to check just bracket bracket zero. Uh or bracket zero bracket.
But how do you get back? Can you do bracket like minus one close bracket? Does that work with rest vectors? Let's find out. Because like like like what I'm what I'm getting at is that if you have the the element access operator that takes an index, it's trivial to get front because it's always zero. But what about back?
I don't think that's that's possible. Like the borough the borough checker. So yeah, let's first uh let's first commit this. Use Vec because I'll even put your name because B Lelbeck B Lelbeck is uh good friend. We push that. Boom. All right, now back to Gobolt. And we haven't even gotten to the main, so this is this is great because we're we're closing in on a second episode here, and then we're gonna be able to dedicate a whole, maybe not a whole, uh, but a third episode to um the scan algorithm, which is really what I wanted to get to. So Bryce asks, what happens if you go out of bounds? Let's see. We go to V bracket 3, and I'm guessing this is not gonna compile. Thread main panicked at index out of bounds.
