Algorithms + Data Structures = Programs - Episode 101: C++ Developers Try Rust!
Episode Date: October 28, 2022In this episode, Bryce and Conor live code some Rust!Link to Episode 101 on WebsiteTwitterADSP: The PodcastConor HoekstraBryce Adelstein LelbachShow NotesDate Recorded: 2022-10-27Date Released: 2022-1...0-28CityStrides.comRust Podcast Twitter PollRust Programming LanguageC++ std::vector::frontRust std::iter::IntoIterator::into_iterRust std::option::Option::unwrapRust std::iter::Iterator::nextAccessing First Element of Vec in Rust (Godbolt)JT on TwitterMara Bos on TwitterJakt Programming LanguageRefactor of Rust vec[0] CommitIntro Song InfoMiss You by Sarah Jansen https://soundcloud.com/sarahjansenmusicCreative Commons — Attribution 3.0 Unported — CC BY 3.0Free Download / Stream: http://bit.ly/l-miss-youMusic promoted by Audio Library https://youtu.be/iYYxnasvfx8
Transcript
Discussion (0)
and the compiler is hugging me and basically holding my hand as I learn the language.
I feel like the Rust compiler hugs you while you're learning,
and the C++ compiler actively punches you in the face.
I hope you're still recording because that was... 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 said we'd never do this again, but I might need to join from my other laptop.
But first things first.
Actually, let me first join.
This is going to be exciting, folks.
We're going to start off with a Twitter poll.
We haven't done a Twitter poll, I feel like, in months.
Somehow it's not going to end well for me.
No, no.
It actually has 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 chocolate 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've run on if you sync it with your Strava or Garmin.
And I've recently become... And then it ranks you out of all the people on the website of how many roads you've run globally.
And I have recently become addicted to this.
Anyways, 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.
All right.
Let me share my screen.
First, we're going to 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...
We'll keep Bryce on the edge of his seat.
Ooh, we got some notifications first.
Should we read the notifications?
We are such a distractible podcast.
Yeah.
I mean, chaos with sprinkles of information.
Stick with the...
All right, we'll stick with the tweet.
Here we go.
Oh, look, we literally just got a notification.
People...
Oh, no, it's five hours ago.
Here's the tweet.
You're like my dog with a ball.
It's like, she could be doing...
She could be eating.
She could be sleeping.
It's like, she could be doing anything.
It's like, ball, ball, ball.
Yeah, it's like that dog from Up.
Anyways, the Twitter poll.
Here we go.
Poll.
Ask a question.
How do we ask this?
What?
Actually, it's really, it's a poll.
Maybe it's a poll from my, so option one is ADSP temporarily becomes a, there's only 25 characters.
What are you trying to pull?
So there's like four other podcasts I want to start.
And one of them is a Rust podcast.
But I could start a Rust podcast or ADSP could just briefly become a Rust.
I want to learn Rust.
Yeah.
All right.
So that's what the poll is about.
But apparently each choice, each option is only allowed 25 characters.
Why are we taking a poll?
This is not a democracy.
The listener gets what the listener wants.
Or the listener doesn't get what the listener
wants listen it's what whatever we want i can't believe there's only there's only 25
characters i'm getting i'm getting chocolate not crunchy chocolate so it won't mess with the uh
becomes uh
it is in a paper bag which is not great for the audio. Oh, I also have caramels.
Those are probably a better idea.
I need help.
I'm two characters.
I need two more characters for the S and the T.
What's a shorter word for becomes?
ADS.
This is.
ADSP.
You're doing this wrong.
You put the options in the body and then you have like...
Oh, yeah.
That's a good point.
What should...
Don't you know how to tweet, bro?
What do listeners prefer?
A, ADSP becomes...
We'll say temporarily.
Temporarily, temp, I can't spell temp,
temporarily becomes a at rustling podcast.
Or B, at code report starts a new wrestling only
podcast.
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 not chocolate.
It's caramel.
It's very good. I don't. It's not chocolate. It's caramel. It's very good.
I don't think there's a point in doing this because I think we should just do A.
I mean, we can just do A.
I still want to send the poll, though.
Why do you want to send the poll?
I mean, because I've already had—
Because either— What are the possible outcomes here?
Either you get back a supportive answer,
and we're going to do it anyways,
so it doesn't influence the decision.
All right.
Or you get back an answer that,
oh, this thing that we're going to do anyways
is going to offend our listeners.
I mean, I might still... If people want a whole new podcast, this thing that we're going to do anyways is going to offend our listeners.
I mean, I might still... If people want a whole new podcast,
we could do both options.
That's what I'll put.
I'll put C both.
Let's go to the live coding section.
I'm sending the poll out.
I'm sending the poll out.
A, B, C, seven days.
Anything you want to modify before I click tweet?
As a chair with experienced polling groups of people,
I think this is a poll that you don't need to take.
But you don't want to change it at all.
You just don't think it should be sent.
Yeah.
All right.
I'm clicking tweet.
You ready?
Sure.
It's sent. All right. Now I'm going to take my hoodie off to become more comfortable for this live coding thing one sec all right let's talk about should we ask a question first let's
ask a question um i don't know 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 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 front.
One.
Pardon?
One.
I know that this, like,
this has to be a trick,
but one?
It is not one.
I mean, just the way you set that up,
I mean, of course,
I know it's wrong,
but that makes me sad and troubled.
Let's actually, we'll do this. Three? I know it's wrong that makes me sad and troubled let's actually we'll do three I think it is I think it is three so let's go to God I knew it I
knew it couldn't be two either because yeah if it's just one or two it wouldn't
be amusing enough so we're in goblet now and uh let v equals vec exclamation mark
one two three exclamation mark mean i know nothing about rust so this podcast is going to quickly
become connor teaches brice rust yeah let's let's uh well let's um, let's put a disclaimer up front.
Connor does not know Rust.
Everything that I say here...
You know more than me.
...could be potentially false.
But my understanding is that the exclamation point means it's a macro.
So both...
Why is VEC a macro?
That's just the syntax they use to indicate to the reader of the code and
probably the compiler as well,
that you are in macro land right now.
So VEC.
No,
no,
no.
But like what I,
I understand that.
Why is,
why does VEC have to be a macro?
Oh,
because they don't have like a shortcut initialize initializer or list syntax
for creating a list.
So this is like the shortest way you can construct a list of numbers.
It makes sense.
It makes sense.
I think technically there is a literal array equivalent to the std array.
But this is like, you know, for dealing with 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 1, 2, 3.
So my understanding is that 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. then if we put i have a dumb question
is there no like you can't just do like v open square brackets zero closed square brackets no
like you wouldn't see plus plus so there's there's no like random access operator operation on a vector.
The problem, I'm sure that you can define one of those,
but the problem is that safety is everything in Rust.
Yeah, I had a feeling that this was the reason why.
And so like next, I believe returns,
basically it's the equivalent of your like plus plus
operator 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,
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,
paren paren,
dot next,
paren paren,
dot unwrap, paren paren. Is it possible that we're, that we're holding this wrong? whatever the name of your vector is,.intoiter.next.unwrap.semicolon.
Is it possible that we're holding this wrong?
Holding this?
Yes, is it possible that we're holding this wrong
and that in the Rust philosophy
that doing this of getting the first element
of a vector like this is not the idiomatic thing.
I mean, let's go to Stack Overflow.
How to get the first element from vector in Rust, which is just what I did a few days ago.
I don't know what this is.
Return first item of a vector.
This is defining your own function.
Yeah, but what I'm getting at is like, is there like
is the reason
that this is unwieldy
because this is not the way that
you should
write Rust code? Maybe.
I mean, but like literally
the second top
answer on this is using
into iter and then.enth.
And I believe.enth is...
What does into iter do?
It turns it into an iterator.
It basically turns it into something that implements the iter traits,
which is sort of equivalent to ranges.
So you can call a bunch of algorithms on it.
You know, sort, reverse, et cetera, et cetera.
Anyways, this is just my first entry into Rust because I feel like...
So, okay, I have some more questions.
Like, couldn't you have an element access operator
like you do in C++,
but one that just returns you a maybe?
Yes, I believe.
Okay, so why...
There must, like...
I'm sure we're not the first people to come across this.
There must be a principled reason why Russ doesn't do this.
Well, actually, first.
Can we get, isn't Mara like the Russ Standard Library person?
Mm-hmm.
Can we get her on the podcast?
I don't know.
I know Jane is super involved, and we know her.
We can get Jane on.
Also, too, we should definitely have on JT, Jason's cousin,
because he works both on Rust and on Yacht, or Yacht, or Jacked,
or whatever the language that's in Serenity OS.
So we can ask about two languages at once.
Does Rust have bracket?
Yeah, Mara
is the Rust slang library
team lead. So we
should have her on. Does Rust have
bracket?
So it
actually looks like it might not.
It might not actually have that.
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 and c plus plus
an app could return you a rust maybe it could take like an index and return you a rest maybe
like why doesn't why doesn't rust have this let's go to the ap. I suspect it's...
I mean, you also have to understand is that...
Hang on, hang on.
I see a vec 0 there.
Where?
Like...
0?
I see a vec, open brackets, 0, close brackets.
Okay, let's go try that.
We're not going to have content for an episode.
Oh, what the F?
What the actual F?
All right, folks.
Did you not try this?
Well, we're not airing this.
We're going to need something else. No, we're definitely, what do you mean? We're definitely, why would we not try this uh well we're not we're not we're not airing this we're gonna need something else no we're definitely mean we're definitely why would we why would we not air this
why would we not air this um uh so let's let me just stash let me stash my current changes
um and you really want the world to know that you uh man why do i shouldn't have to man that
this is bad shame on you bryce shame on uh you like basically trying to make a beginner developer
that's learning about a technology not 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 is possible, but let's let's go and check if this actually 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.
Yeah, don't save.
Go back here.
All right, 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 all right we will do that also how wonderful i mean bryce has left his computer so we're not really live coding anymore um no no i'm
i'm watching i'm just like like, I'm on the ground.
I'm looking up.
Look, the dog needs attention.
I can do two things at once.
And this is what is amazing about Rust.
This is compiler just... Yeah, I mean, that's a pretty good diagnostic.
I mean, especially...
It just told me basically exactly what to do.
And you are
incredibly correct look at this so we're now we're gonna do a little nice little beautiful
refactor here i want to i want to i want to i want to take a step back and um
i want to understand what did did you not try this i didn't know that this was possible
but but why didn't you try it?
Because that's what you're used to doing, right?
I feel like it didn't work.
Okay, because I feel like you must have tried it.
It works here as well.
And...
It is funny how often this happens
where you think you've discovered
some problem or something and, um, and then you go to show somebody else.
And then as you're showing it to somebody else, it's like the, the, 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 explaining something
to other people.
Well, I mean, this is the power of pair programming.
Yeah.
So look at this.
Now we've got a diff replacing every single
into, it, or next unwrapped with just angle bracket zero.
Although note, and that's the thing.
So in Rust, my understanding is-
I think i know
i think i now know what happened what 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. My gut reaction was to
reach for angle bracket zero if I want the first element of a thing. Because to me, from more of
the systems programming background, it's just the first element in a contiguous array of memory.
Or really it's more the pointer syntax of which I am used to working with
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.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 that 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 frowned next but next didn't work.
And in order to use next,
you had to call into iter,
but 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.
Yeah.
Bracket, zero, bracket.
But how do you get back?
Can you do bracket,
like, minus one, close bracket?
Does that work with West vectors?
Let's find out.
Because, like,
what I'm getting at is that
if you have the element access operator
that takes an index,
it's trivial to get front
because it's always zero.
But what about back?
Well, first, let's commit this.
Also, what is the Rust vector element
access return? What happens if it's out of bounds?
I don't think that's possible.
Like the borrow checker. So yeah, let's first
commit this. Use vec, because I'll even put your name,
because B. Lelback,
B. Lelback is a good friend.
We push that.
Boom, all right, now back to Godbolt.
And we haven't even gotten to the main,
so this is great,
because we're closing in on a second episode here.
And then we're going to be able to dedicate a whole, maybe not a whole,
but a third episode to 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 going to compile.
ThreadMain panicked at index out of bounds.
Ah, it compiled and ran and led to a runtime error.
But I thought Rust was supposed to protect us from these things.
Okay, now try negative one.
I want to see what happens, whether that gives you back.
Negative integers cannot be used.
This did not compile.
Okay, so then why isn't there a back?
Well, we didn't actually finish looking at the API.
So there's a.len which we can use.
This is a lot of things.
Why does it need so many things?
V.len minus one.
So V bracket V dot len paren paren minus one bracket,
which is what you would do in many languages.
Pretty ugly.
I feel like they should make negative one.
Yeah, it's potential.
It's a possibility in the future.
All right, that'll wrap up.
All right, go ahead.
You got a question before we move? Okay, answer the answer is that if you go out of
bounds it panics um and there's no back okay well also if you are a rust informed individual
i will leave a link in the show notes of this episode to the commit that I pushed to this
little toy repository.
If you want to go look at this change and potentially my code that I had before, is
it more idiomatic, which is why it was on stack overflow.
And the bracket zero bracket to get the first one is considered bad because it panics at
runtime.
But anyways, this has been educational.
Thanks for listening.
We hope you enjoyed and have a great day.