Podcast Lookup

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

Twitter

Show Notes

Date Recorded: 2022-10-27
Date Released: 2022-10-28

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 publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Episode 101: C++ Developers Try Rust!

00:00—
  1. Conor

    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.

  2. Bryce

    Uh, this is this somehow it's not gonna end well for me.

  3. Conor

    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.

  4. Bryce

    Um we are such a distractable podcast.

  5. Conor

    Yeah, we I mean chaos with sprinkles of information.

  6. Bryce

    Stick with the stick with the Alright, we'll stick with the tweet.

  7. Conor

    Here we go. Oh, look, we literally just got a notification. People oh no, it's five hours ago. Here's the tweet.

  8. Bryce

    You're you're 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 then it's like ball, ball, ball?

  9. Conor

    Yeah, it's like uh it's like that dog from up. Anyways, the Twitter poll. Here we go. Uh poll. Ask a question. How do we ask this? What? Actually, it's really it's a poll. Is it should a maybe it's a poll from my but so option one is ADSP temporarily becomes uh There's only 25 characters!

  10. Bryce

    What are you what are you trying to pull?

  11. Conor

    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.

  12. Bryce

    So why are why are we taking a poll? This is not a democracy. The listener gets what the listener wants. Or what the listener doesn't get what the listener wants, the listener gets what whatever we want.

  13. Conor

    I can't believe there's only there's only 25 characters.

  14. Bryce

    Hang on, I'm 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, it's not great for the audio. Ooh, I also have caramels. Those are probably a better idea.

  15. Conor

    I need help. I'm I'm two characters I need two more characters for the S and the T. What's a shorter word for becomes? ADSP? This is ADSP.

  16. Bryce

    You're doing this wrong. You put the options in the body and then you have like Oh yeah, yeah, that's a good point. What should Don't you know how to tweet, bro?

  17. Conor

    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.

  18. Bryce

    It's not chocolate, it's caramel. It's very good. Um I don't think there's a point in doing this because I think we should just do A.

  19. Conor

    I mean we can just do A. I still want to send the poll though.

  20. Bryce

    Why do you want to send the poll?

  21. Conor

    Uh I mean because of our.

  22. Bryce

    Because either either What what are the possible outcomes here? Either you get back a supportive answer, and we're gonna do it anyways. So it doesn't influence the decision.

  23. Conor

    Alright.

  24. Bryce

    Or you get back an answer that, oh, this thing that we're gonna do anyways is gonna offend our our listeners.

  25. Conor

    I mean, I might still if people want a whole new podcast, we could still we could do both options. That's what I'll put. I'll put C both.

  26. Bryce

    Let's go to the live coding, the live coding section.

  27. Conor

    I'm sending the poll out. I'm sending the poll out. Um A, B, C, seven days. Anything you want to modify before I click tweet?

  28. Bryce

    As a as a chair with experience polling groups of people. I think this is a poll that you don't need to take.

  29. Conor

    But you don't want to change it at all. You just don't think it should be sent.

  30. Bryce

    Yeah.

  31. Conor

    Alright, I'm clicking tweet. You ready?

  32. Bryce

    Sure.

  33. Conor

    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?

  34. Bryce

    One? I know that this like this has to be a trick, but but one?

  35. Conor

    It is not one.

  36. Bryce

    Um I mean, just the way you set that up. I mean, of course, I knew I was wrong, but yeah, makes me sad and troubled.

  37. Conor

    Let's actually we'll do three? Uh I think it is I think it is three. Um so let's go to Gobble.

  38. Bryce

    I know it couldn't be two either, because yeah, if it was just one or two, it wouldn't be amusing enough.

  39. Conor

    So we're in Gobble now. And uh let V equals Vec exclamation mark one, two, three.

  40. Bryce

    What does exclamation mark mean? I know nothing about Rust. So this podcast is gonna quickly become Connor teaches Bryce Rust.

  41. Conor

    Yeah, let's let's uh well let's um clear it, let's put a disclaimer up front. Connor does not know Rust. Everything that I say here could be potentially false. But my understanding is that the exclamation point means it's a macro.

  42. Bryce

    So both Why is Vec a macro?

  43. Conor

    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.

  44. Bryce

    So VEC No, no, no, but like why I I understand that. Why is why does Vec have to be a macro?

  45. Conor

    Oh, because they don't have like a shortcut initialize initializer list syntax for creating uh a list. So this is like the shortest way you can construct a list of numbers.

  46. Bryce

    Yeah, it makes sense. It makes sense.

  47. Conor

    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.

  48. Bryce

    And then if we put I have a dumb question. Is there no like you can't just do like V open square brackets zero close square brackets? No, like you would in C. So there's there's no like random access operator operation on a vector.

  49. Conor

    The problem I'm sure that you can define one of those, but the problem is that safety is everything in Rust.

  50. Bryce

    Yeah, I was I I had a feeling that this was the reason why.

  51. Conor

    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.

  52. Bryce

    Is it possible that we're that we're holding this wrong?

  53. Conor

    Holding this?

  54. Bryce

    Like that yes, is it possible that we're holding this wrong and that this is that in the the Rust philosophy that doing this of like getting the first element of a vector like this is like not the idiomatic thing.

  55. Conor

    I mean let's go to sta let's go to Stack Overflow. How to get the first element from vector in Rust, which is just what I did, you know, a few days ago. I don't know what this is. Return first item of a vector. This is defining your own function.

  56. Bryce

    Yeah, yeah, but what 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?

  57. Conor

    Maybe. I mean, but like literally the second top answer on this is using into iter and then dot nth, and I believe dot nth is.

  58. Bryce

    What does into iter do?

  59. Conor

    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, etc. etc.

  60. Bryce

    Anyways, this is just this is just my first like entry into Rust because like I feel like So So okay, I I I have some some more qu like couldn't that couldn't you have an in an a um element access operator like you do in C ⁇ , but one that just returns you an uh a maybe?

  61. Conor

    Yes, I believe.

  62. Bryce

    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.

  63. Conor

    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?

  64. Bryce

    Yeah, yeah. So we should have her on.

  65. Conor

    Does Rust have bracket? Um so it actually does looks like it might not. It might not actually have that.

  66. Bryce

    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?

  67. Conor

    Let's go to the A. I mean you also have to understand is that Oh hang on, hang on.

  68. Bryce

    What I see a VEC zero there.

  69. Conor

    Where?

  70. Bryce

    Like Zero? I see a VEC open brackets, zero, close brackets.

  71. Conor

    Okay, let's go try that.

  72. Bryce

    We're not gonna have content for an episode.

  73. Conor

    Oh, what the F? What the actual F? Alright.

  74. Bryce

    Folks Did you not try this? Uh Well, we're not we're not we're not airing this. We're gonna need something else.

  75. Conor

    No, we're definitely what do you mean? We're definitely- 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.

  76. Bryce

    Um, you really want the world to know that you uh Man, why do I shouldn't have to man, that this is bad.

  77. Conor

    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.

  78. Bryce

    Um, no, I'm I'm watching. I'm just like I'm I'm on the ground, I'm looking up. Um look, the dot the dog needs attention. I can do two things at once.

  79. Conor

    And this is what is amazing about Russ.

  80. Bryce

    Is this compiler just Yeah, I mean that's it's pretty good diagnostic, I mean, especially computer.

  81. Conor

    It just told me basically exactly what to do. And um you are incredibly correct. Look at this, so we're now we're gonna do a little nice little beautiful refactor here.

  82. Bryce

    But I I wanna I wanna I wanna I wanna take a step back and um I wanna understand why did did did you not try this?

  83. Conor

    I didn't know that this was possible.

  84. Bryce

    But but why didn't you try it? Because like that's what you're used to doing, right?

  85. Conor

    Uh I feel like it didn't work.

  86. Bryce

    Okay, because I feel like you must have tried it.

  87. Conor

    It works here as well.

  88. Bryce

    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.

  89. Conor

    Well, I mean this is this is the power of pair programming.

  90. Bryce

    Yeah.

  91. Conor

    So look at this. Now we've got a diff replacing every single into iter next unwrapped with just angle bracket zero. Although note and that's the thing, so in Rust, if my understanding is I think I know.

  92. Bryce

    I think I now know what happened.

  93. Conor

    What?

  94. Bryce

    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.

  95. Conor

    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.

  96. Bryce

    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?

  97. Conor

    Well, first, let's uh commit this.

  98. Bryce

    Also, what what is um what is the rest vector element access uh return? Like what happens if it's out of bounds? I would like to.

  99. Conor

    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.

  100. Bryce

    Ah, it it compiled and uh and ran and led to a runtime error. But I thought Rust was supposed uh protect us from these things. Okay, now now try negative one. I want to see what happens, whether that gives you back.

  101. Conor

    Negative integers cannot be used. This did not compile.

  102. Bryce

    Um, so then why why why doesn't why doesn't uh why isn't there a back? Why isn't there a back?

  103. Conor

    Well, we didn't actually finish looking at the the API. Um so there's a dot len which we can use.

  104. Bryce

    This is a lot of things. Why does it need so many things?

  105. Conor

    V dot 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.

  106. Bryce

    I feel like they should make negative one.

  107. Conor

    Yeah, it's potential. It's a possibility in the future. Alright, that'll that'll wrap up. Alright, go ahead. Let's you got a question before.

  108. Bryce

    Okay, so that's the answer. The answer is that if you go out of bounds, it panics. Um and there's no back. Okay. Well.

  109. Conor

    And the bracket zero bracket to get the first one is considered bad because it panics at runtime. But um, anyways, this has been educational. Thanks for listening. We hope you enjoyed and have a great day.