SE Radio 659: Brenden Matthews on Idiomatic Rust
Brenden Matthews, a seasoned software engineer, entrepreneur, and author of the Idiomatic Rust and Code Like a Pro in Rust books (both from Manning), speaks with SE Radio host Gavin Henry about Idiomatic Rust. They start with a look at what "idiomatic" means, and then discuss Generics, Traits, common design patterns you'll see in well written Rust code, and anti-patterns to avoid. Matthews suggests some tools that can help you immediately write idiomatic Rust, as well as what building blocks can also help. This episode examines what Generics are and how they compare to other languages, as well as what Traits are, how macros help, what a Fluent Interface is, and why unwrap() is bad. They also discuss what code smells to look out for, Clone, Copy, and a really nice place to go read real-world Idiomatic Rust code. Brought to you by IEEE Computer Society and IEEE Software magazine.
Brenden Matthews, a seasoned software engineer, entrepreneur, and author of the Idiomatic Rust and Code Like a Pro in Rust books (both from Manning), speaks with SE Radio host Gavin Henry about Idiomatic Rust. They start with a look at what “idiomatic” means, and then discuss generics, traits, common design patterns you’ll see in well written Rust code, and anti-patterns to avoid. Matthews suggests some tools that can help you immediately write idiomatic Rust, as well as what building blocks can also help. This episode examines what generics are and how they compare to other languages, as well as what traits are, how macros help, what a fluent interface is, and why unwrap() is bad. They also discuss what code smells to look out for, Clone, Copy, and a really nice place to go read real-world Idiomatic Rust code.
Brought to you by IEEE Computer Society and IEEE Software magazine.
Show Notes
Related Episodes
- SE Radio 562: Bastian Gruber on Rust Web Development
- SE Radio 490: Tim McNamara on Rust 2021 Edition
- SE Radio 644: Tim McNamara on Error Handling in Rust
Other References
Transcript
Transcript brought to you by IEEE Software magazine and IEEE Computer Society. This transcript was automatically generated. To suggest improvements in the text, please contact [email protected] and include the episode number.
Gavin Henry 00:00:18 Welcome to Software Engineering Radio. I’m your host Gavin Henry. And today my guest is Brenden Matthews. Brendan is a seasoned software engineer, entrepreneur and active open-source contributor with over 20 years of industry experience. He has been working with Rust since his early days contributing to various Rust tools and open-source projects and is the author of two books, Idiomatic Rust and Code Like a Pro in Rust , both from Manning. Brenden is also the creator of Conkey, a popular system monitoring tool and a longtime member of the Apache Software Foundation. Brendan, welcome to Software Engineering Radio. Is there anything I missed in your bio that you’d like to add?
Brenden Matthews 00:00:57 No, that’s pretty good. Nice work.
Gavin Henry 00:00:59 Two books under your belt’s, pretty impressive. One’s impressive enough. To lay the foundation I’d like to start with an overview of what Idiomatic Rust is. So what does this mean?
Brenden Matthews 00:01:11 It’s a good question. So I suppose, I mean forget Rust for a second, to write any software in a way that is idiomatic is to do it such that the way you’re using it is in line with the best practices and patterns that are well known commonly used generally for whatever that language or tooling is. And a lot of that is pretty subjective I would say. Right? And these things are also like context dependent. Just as an example, every different company tends to have their own ideas about how to do like coding style and so on. And some companies will say like we want all our variable names Camel Case and others might prefer a snake case or whatever. And often it’s also just like code-based dependent. Like if you’re doing open-source stuff, generally you follow whatever pattern that project that you’re contributing to already uses.
Brenden Matthews 00:02:13 And so anyway, in the case of Rust, Rust has its own idioms and patterns that make sense in a Rust world. Rust specifically because it introduces some new ideas about how to do programming. I would say it means there are some new idioms and patterns that you would not have seen in any other programming languages unless you’ve used ones that have very similar features to what you have in Rust, which other languages have the features that Rust has like at least nowadays it’s not like Rust is the only language in the world that has traits or whatever, but if you’re coming from say Python to Rust, then it’s probably a wildly different world from what you’re used to. So how do you get up to speed very quickly such that you can write Rust that makes sense in a Rust world I guess.
Gavin Henry 00:03:09 And you mentioned sort of standards and styles there within companies. Is idiomatic in relation to any language the same as a code style or code standards that you’ve set or are they different?
Brenden Matthews 00:03:22 It’s a good question. So I spend some time talking about this in my book actually. So one thing is defining the difference between a pattern or a design pattern and an idiom. And generally speaking, at least the way that I define it is that idioms relate to things like code formatting more like stylistic things. An example like I already used this, how do you format your variable names to use snake case or camel case in the case of Rust, Rust has like a bit of a quirky pattern or idiom that it uses, which is that generally most like variables are snake case whereas names of structures or enums or whatever their definitions are going to be. I guess it’s not Camel case but whatever is the casing where like the first character is uppercase, maybe that is just Camel case. Although when I think of Camel case I usually think like the first character is lowercase and in any case, and then there are like few other ones like if you define a global variable in Rust, then that’s supposed to be all uppercase separated by underscores, whatever that’s called.
Brenden Matthews 00:04:37 And so these idioms like if you think about it actually doesn’t really matter how you format your variable names in real life, right? Like once the code gets run through the compiler and comes out the other end, like these things don’t really matter. So in that sense it’s just purely stylistic, right? That’s totally different from something like design pattern which design patterns relate more to how you structure the code in like more of an architectural sense and that can affect actual outcomes like performance and algorithm runtimes and stuff probably trivially, but it can have some effect to a certain degree, especially if you’re talking about getting into like CPU caches and stuff.
Gavin Henry 00:05:24 Yeah, we’re going to touch on some of the patterns that you mentioned in your book in the next couple of sections you touched upon talking about what should be the right way to do things like the cases or the style and when in fact it doesn’t really matter. And that’s something that I’ve come across myself because I’ve been learning Rust for a while now and you just have this inbuilt feeling that you want to do it right, what’s the right way to do it. So are we bad programmers if we don’t write idiomatic Rust?
[...]