103 avsnitt • Längd: 15 min • Månadsvis
Deep dive teaching episodes, news updates, and interviews with people around the Rust community.
The podcast New Rustacean is created by Chris Krycho. The podcast and the artwork on this page are embedded on this page using the public podcast feed (RSS).
A story and a dream (and the promise of Rust): the final episode of New Rustacean!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
WASI, Option::copied
, and the future of async/await syntax!
Thanks to Parity for sponsoring the show and hiring Rust developers!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Automatic generation of FFI types between Rust and C APIs.
Thanks to Parity for sponsoring the show and hiring Rust developers!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Exposing Rust types and functions to C API consumers.
It’s impossible to make the declarations below follow the order I talked through them on the recording without also making them horrible to read, so just use this outline instead:
add_in_rust
Point
union
OpaquePoint
Thanks to Parity for sponsoring the show and hiring Rust developers!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Item visibility and pub(<restricted>)
as API design tools.
The easiest and most effective way to understand the example in this case will simply be to look directly at the source code. You can read the docs for each of the nested modules, but you’ll be doing a lot of navigating around for that.
Also, I am using Cargo’s --document-private-items
flag, so that you can see all the items in all the modules, even those which are not public, but note that usually you would not see docs for those!
Thanks to Manning for sponsoring the show and giving all of you a 40%-off discount on their whole store (but especially their WebAssembly in Action MEAP) at deals.manning.com/new-rustacean!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Moar const fn
, some Pin
, and alternative Cargo registries!
Thanks to Parity for sponsoring the show and hiring Rust developers!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Using Rust’s Foreign Function Interface (FFI) with C!
The code samples here directly match the things I described in the show, so you will likely want to look at add
and ffi::add
, then Point
, translate
, and ffi::translate
in that order.
Other helpful Rust FFI discussions:
Thanks to Manning for sponsoring the show and giving all of you a 40%-off discount on their whole store (but especially Carol Nichols’ and Jake Goulding’s Rust in Motion video content and the Rust in Action MEAP!) at deals.manning.com/new-rustacean
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
How I make the show, and why you won’t find it on Google Play, Spotify, or Stitcher.
Thanks to Parity for sponsoring the show and hiring Rust developers!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
A story about parsing command-line arguments manually and some thoughts on “rookie mistakes.”
Thanks to Parity for sponsoring the show and hiring Rust developers!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Associated items: functions and methods, constants, types, and (very briefly!) GATs.
Iterator
sourceThanks to Manning for sponsoring the show and giving all of you a 40%-off discount on their whole store (but especially Carol Nichols’ and Jake Goulding’s Rust in Motion video content and the Rust in Action MEAP!) at deals.manning.com/new-rustacean
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
dbg!
, unified paths, more places you can use Self
, and a bunch of const fn
stabilizations—plus some neat community highlights!
Thanks to Parity for sponsoring the show again. Go check out their Rust jobs!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Stabilizing rustfmt, clippy, tool lints, and const fn
(all in both editions!), and the 2018 Edition-specific features: syntax changes and non-lexical lifetimes!
const fn
in the reference and “const contexts”Thanks to Parity for sponsoring the show again. Go check out their Rust jobs!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
An overview of the edition, and some of the improvements that are available in both the 2015 and 2018 editions: better lifetime elision, some Cargo features, and some library stabilizations.
Thanks to Parity for sponsoring the show again. Go check out their Rust jobs!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Things we mentioned on the show:
Thanks to Manning for sponsoring this episode; don’t forget to grab some of their content at 40% off using the code podnewrust18
!
Thanks to Parity for sponsoring the show again. Go check out their Rust jobs!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
An intro to unsafe
Rust and Rust’s idea of safety.
A quick correction: on the show I said that a trait needed to be unsafe when it had an unsafe fn
method. This isn’t correct: safe traits can have unsafe methods, and unsafe traits can exist without any methods at all (as implied by my reference to Send
and Sync
). You can see this in practice in the following example, which compiles just fine!
trait ASafeTrait {
unsafe fn unsafe_method() {}
}
unsafe AnUnsafeTrait {}
The idea of an unsafe
trait is that it has some conditions which you must uphold to safely implement it – again, just as with Send
and Sync
. In the case of most traits, this will be because some trait method has invariants it needs to hold else it would cause undefined behavior. For another example of this, see the (unstable as of the time of recording) trait std::iter::TrustedLen
.
Thanks to Rust language team member @centril for noting this to me after listening when I was recording the show live!
unsafe
let mut f = String::from("foo");
unsafe {
let borrowed = &mut f;
let borrow_again = &f;
println!("{}", borrowed);
// This would be unsafe and throw an error:
// println!("{}", borrow_again);
}
let f = Box::new(12);
let mut g = Box::into_raw(f);
g = &mut (g + 10);
Thanks to Parity for sponsoring the show again. Go check out their Rust jobs!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Procedural macros, better paths, and the Rust 2018 beta!
Rust 1.29:
Rust 1.30:
Thanks to Parity for sponsoring the show again. Go check out their Rust jobs!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Musings inspired by a project translating TypeScript to Rust
Thanks to Manning for sponsoring the show and giving all of you a 40%-off discount on their whole store (but especially Carol Nichols’ and Jake Goulding’s Rust in Motion video content and the Rust in Action MEAP!) at deals.manning.com/new-rustacean
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
#[doc(include = “../docs/bonus/translating-between-languages.md”)] pub struct Script;
What burnout is, the many ways it can happen to us, and what we can do about it.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Stable `#[global_allocator]`, more Rust 2018 Edition schedule news, video learning resources, and a static site generator!
wee_alloc
:
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
How Rust both borrows from FP languages and charts its own, very different course.
Sponsored by Parity Technologies! Parity is hiring Rust developers so if you’re interested, you should check out their job listings!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Closure traits, `impl trait`, `dyn trait`, and object safety!
Sponsored by Parity Technologies! Parity is hiring Rust developers so if you’re interested, you should check out their job listings!
You can see all of the pieces of the final example described in the show here (and the module has the required definitions for Point
).
let points = vec![
Point { x: 1.0, y: 2.0 },
Point { x: 12.0, y: 4.3 },
Point { x: -5.4, y: 18.7 },
];
let origin = Point::default();
// This is the version we start with. It works fine, but it's not elegant.
let distances_inline: Vec<f32> = points
.iter()
.map(|point| {
let change = point - &origin;
(change.x.powi(2) + change.y.powi(2)).sqrt()
})
.collect();
// This version is *much* cleaner!
let distances_impl: Vec<f32> = points.iter().map(distance_from_impl(&origin)).collect();
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Stable SIMD, `dyn trait`, `rustfix` and the alpha release of the Rust 2018 Edition Preview!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Operators as sugar for traits, traits as generic constraints, monomorphization, and universal and existential types.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
wasm intro, wasm-bindgen
, and wasm-pack
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
impl trait
, match
on references, Result
s from main
, and more. A good way to mark three years since Rust 1.0!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Defining and using your own traits, using other crates' traits, and the orphan rule.
Traits—
Also of interest: specialization:
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Paths and matches and SIMD, cargo new changes, and tons of community-driven learning materials!
#[repr(align)]
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
My Rust Belt Rust 2017 talk!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Performance wins, incremental compilation, and the Rust 2018 Roadmap and Epoch.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
The library for serialization and deserialization in Rust.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
The “marker traits” that Rust uses for safe concurrency.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Getting Diesel to 1.0, writing docs and exposing problems with the API, improving Diesel in the future, and thinking about API design for open source libraries in general.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Growing Diesel's community, self-imposed technical challenges, and some of the ways Diesel has contributed to the Rust ecosystem.
byteorder
crateimpl
period
Matt’s experience porting wget to Rust.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Lee’s experience designing a new website for Rust.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Anna's experience learning Rust while porting a Raspberry Pi Python project as part of the Increasing Rust’s Reach 2017 program.
burntsushi
)
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
A micro-interview recorded at Rust Belt Rust 2017, in Columbus, Ohio, October 27–28.
Quality of life improvements, Failure
, wasm, and rustdoc fun.
Failure
crate
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Two years and fifty episodes of New Rustacean—time to celebrate with stickers and shirts!
Get them here! – available till Oct 9, 2017 at 8:00 PM EDT.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Safe, threaded, parallel code in Rust!
RefCell
s and code smells] – on mutabilityhttps://www.newrustacean.com/show_notes/e016/index.html ["embarassingly parallel"]: https://en.wikipedia.org/wiki/Embarrassingly_parallel
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Associated constants, conference season, meetups, and more!
newrustacean
for 20% off!)(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
My experience with ember-cli-typescript as an example: we're all just people muddling along and doing our best.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Using type aliases and creating custom type wrappers for more expressive and safer code.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Growing Rust's diversity to help Rust grow.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
An accessible, well-designed web framework in Rust!
Future work on the RLS, in Rust itself, and in Servo.
Building the Rust Language Service:
Working on Servo:
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Rust as the fusion of systems and high-level programming languages, and the RLS.
Building the Rust Language Service:
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Background, TypeScript, coming to Rust, and how helpful the Rust community can be.
On Jonathan's programming backstory:
After the transition to working on Rust full-time:
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Where the RLS came from, what it can do, and how you can start using it today!
One major ergonomic improvement to developing in Rust in 2017 is coming via the Rust Language Service: an initiative that lets us share a common core of functionality between every editor – from Vim to VS Code and everything in between. In today's episode, I give some background on it and talk about how you can start using it today!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
How do we organize code in Rust? Where do we break it apart into modules or crates, and why?
Structuring code in a language like Rust can seem a bit more ambiguous than doing the same in a language with classes to attach all our functionality to, but in practice, the concerns are much the same: modules are namespaces, and we group by responsibility. In today's episode, I talk through that philosophy (and give some comparisons to other languages), and then look at what it looks like in practice!
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
On the responsibilities and opportunities we have to help others with our knowledge and abilities.
Many of us have been very blessed with opportunities and support as we learn software. We should go out of our way to share with others in kind. Today, my focus is on teaching, but there are lots of ways to "give back." And I'd love to hear your thoughts and things you're doing in that vein!
keyof
and Mapped Types in TypeScript 2.1 -- the blog post I mentioned writing a few months ago.(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
The final pieces of the story for (single-threaded) memory management in Rust.
Sometimes, we actually do need to copy types. Wouldn't it be nice if Rust gave us a convenient way to do that when it's convenient, or when the cost is low enough that the ergonomic tradeoffs are worth it? Well, perhaps unsurprisingly, it does! The Copy
and Clone
traits, plus the Cow
type, give us everything we need!
The typess
7.2.0.2 Moved and copied types:
When a local variable is used as an rvalue, the variable will be copied if its type implements
Copy
. All others are moved.
Default implementations
Supertraits
from the discussion in the reference (6.1.9 Traits):
Traits may inherit from other traits.... The syntax
Circle : Shape
means that types that implementCircle
must also have an implementation forShape
. Multiple supertraits are separated by+
, traitCircle : Shape + PartialEq { }
. In an implementation ofCircle
for a given typeT
, methods can refer toShape
methods, since the typechecker checks that any type with an implementation ofCircle
also has an implementation ofShape
...
discussion of trait super- and subtyping in the new book (note: still to-be-written at the time this episode was published)
Marker traits
in the reference: 9 Special Traits
Previous episodes on traits:
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
A Command-Line Argument Parser.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Borrow
, AsRef
, and Deref
are a little complicated, but they're well-worth understanding. Together, they give you tools for dealing with everything from HashMap
and friends to conversions involving smart pointer types to easily using String
and str
or Vec
and slice together.
AsRef
, Borrow
, and Deref
:
Borrow
and AsRef
in The Rust Programming Languagecollections::borrow::Borrow
std::convert::AsRef
std::ops::Deref
Rust’s achievements in 2016 and goals for 2017
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Avoiding burnout by taking it a little easier.
Sometimes, the way a podcast stays in existence is by coming out less often. That’s what’s happening here.
Katas—or: learning by doing
One of the best ways to learn is to pick a small problem you have already internalized, and to do it again but in a new language or in a new way in a language you already know.
cargo-incremental
:
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Building (and celebrating) all the little, not-so-glorious pieces of the Rust ecosystem.
We love the Rust compiler team. But there’s more to the Rust community, and more required for Rust to be as great as it can be, than just the language itself. We need to celebrate other libraries, and even the small ones, just as much (and maybe more) than changes to the language. We need to dig in and work on building the whole ecosystem. (The good news is, we are!)
futures-rs
ring
– Safe, fast, small crypto using Rustalexa-rs
– Rust library for building Alexa skillsgilrs
– Game Input Library for Rust(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
A deep dive on references and pointers in Rust.
By listener request, today we look at the syntax and semantics of referencing and dereferencing and the corresponding &
and *
operators.
As was the case with e016, the code samples have little to say in their documentation; reading the code will be necessary for seeing the ideas.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Digging deeper on smart pointers and mutability with Cell
and RefCell
.
What are the Cell
and RefCell
types, and when should we use them?
Today, we follow up both the detailed discussion of smart pointers in e015 and the closely related discussion in Interview 2 with Raph Levien, and look at two types you need to have a good idea how to deal with if you want to use these smart pointer types more ergonomically—that is, how to use them without feeling like you’re beating your head against a wall!
Cell
and RefCell
:
std::cell
module docs (detailed explanation, complementary to the book)Cell<T>
docs (at std::cell::Cell
)RefCell<T>
docs (at stc::cell::RefCell
)(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Raph Levien on Rust’s current strengths and places it can improve
Chris chats with Raph Levien about what inspired him to build a text editor, as well as about where the rough edges in the Rust development story are today, and how we might improve them going forward.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Raph Levien on using Rust to build the Xi editor
Chris chats with Raph Levien about his background in software development, what attracted him to Rust, and how he’s using Rust to build the Xi Editor, a project which aims to be the fastest text editor out there, with native user interfaces and a Rust text engine.
Arc
]RefCell
]Borrow
]AsRef
]The Unix philosophy emphasizes building simple, short, clear, modular, and extensible code that can be easily maintained and repurposed by developers other than its creators. The Unix philosophy favors composability as opposed to monolithic design.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Box
, String
, Vec
, Rc
, and Arc
have this in common: they’re not dumb.
This episode, we take a close look at smart pointer types—from a few we’ve already talked about, like Box
, Vec
, and String
, to some new ones, like Rc
and Arc
.
Box
.Rc
and Arc
.(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
A year in, Rust is changing fast but still stable.
rustup
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Some things matter more than contributing to open-source software in your free time. A lot more.
It’s trendy to ask for open-source work as evidence of your interest in tech and commitment to software development. Trendy and completely wrong. Companies should not demand open-source contributions from their employees, and beyond that, should learn to recognize that profit is not the most valuable thing in the world. People are.Strings
&str
s and Vec
s and slices (and Unicode) – oh, my!
This episode, I take a deep dive on strings in Rust, looking at the differences between String
and &str
, discussing Unicode a bit, and then expanding the discussion to think about how these types relate to the types they’re built on (like Vec
).
str
docs:
String
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Just how good Rust is, and how you can learn it even if you’re busy.
Sometimes life goes crazy and I don’t have time to do all the technical writing required for a full episode, but I can’t get Rust off my mind, so I record an episode like this one. Where I talk a bit about how versatile Rust is and suggest some surprising ways you might be able to use it.
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
Reasoning about and using lifetimes in Rust (and why we need them)
Lifetimes are our way of reasoning about how long a given piece of data is
available and safe to use in Rust. The reason we don't have the dangling
pointer problem is that we do have lifetimes instead. They're not magic,
they're just a bit of semantics and syntax that let us specify the rules for
how long any given item lives, and how long references to data must be valid.
(Thanks to the couple people donating who opted out of the reward tier, as
well. You know who you are!)
What it means to be an expression-oriented language, and how that works out in Rust.
Rust is an expression-oriented language. What does that mean, and how does it play out in Rust? We look at if
and match
blocks, discuss looping constructs, and examine functions, and then widen out to discuss how having an expression-oriented language can change the way we think about programming.
Type systems: strong vs. weak, dynamic vs. static, and degrees of expressivity.
Talking about type systems! A broad and wide-ranging discussion about type systems in general, with specific examples from languages like PHP, JavaScript, Python, C, C++, Java, C♯, Haskell, and Rust!
A comment on the C integer/character string addition example: what’s actually happening there is that the character string is an array “under the covers,” and as such has an address. C silently switches to using the memory address, which is of course just an integer, when you try to add the two together. As I said on the show: the result is nonsense (unless you’re using this as a way of operating on memory addresses), but it’s compileable nonsense. In a stricter and stronger type system, memory addresses and normal numbers shouldn’t be addable!
Sean Griffin on type systems and hopes for Rust's future
Chris chats with Sean Griffin about the tradeoffs between mental overhead and type safety, the expressiveness of different type systems, and some of the places where Rust currently falls down.
Sean noted he could be wrong about IEnumerable<T>
not having a Sum
method in C♯, and post-show research indicated that he was (it's possible it was added after he had stopped doing .NET work, of course). See the documentation for details on how IEnumerable<T>.Sum
it behaves in C♯ if you're curious.
As a related note, I (Chris) have done a little bit of digging on C♯ in the interval and it's fair to say that while a lot of the "ceremony" involved in writing C♯ is annoying, it's much more than just a "slightly nicer Java", and indeed is a much nicer language than my previous, limited exposure had led me to believe. It's no Rust or F♯, but its type system is substantially more capable than Java's.
Sean Griffin on Rust, ORMs, and Web Frameworks
Chris chats with Sean Griffin about his programming background and initial experience with Rust, Rust's appeal, and what he's doing with Diesel and some of his plans for a new web framework in Rust.
Community is one of the most important parts of a programming language community, or indeed any technical community. In this episode, I talk a bit about what happens when you don't have a good community, how Rust's community has done well so far, and then how to keep building a good community and how to build good things as a community.
Macros rule!
show_notes
crate documentation, rather than within the documentation for this module. (See the Rust Book discussion of documenting macros for details.) Even so, the source is still in this module; see the implementations for details.
Last time, we looked at generics and traits at a high level. This time, we dig deeper on traits, looking specifically at std::iter::Iterator
as an example of a powerful trait that can be composed across types, and then at how we might compose multiple traits on a single type.
We also talk about the syntax for traits, the use of marker traits, some of the things you can’t presently do with traits, and even just a smidge about the future of traits in Rust. All that in less than 20 minutes!
You’ll find today’s source example fairly interesting, I think: it’s just one type, but it uses almost every concept discussed on the show today!
In this episode we cover—at a very high level—two more fundamental concepts in Rust programming: generics and traits.
Generics gives us the abilitty to write types and functions which can be used with more than one type. Traits give us the ability to specify behavior which can be implemented for more than one type. The combination gives us powerful tools for higher-level programming constructs in Rust.
Now that we have a handle on how tests work, we’ll use them to validate the behavior of our code going forward. This is great: we can show that the tests do what we think.
To today’s point, though: we actually know even apart from whether the tests run successfully that these generic functions and the associated traits are behaving as we want. Failure with generics is a compile-time error, not a runtime error.
All about testing in Rust! In order, we take a look at:
The detailed code samples for this episode are heavy on showing; because of the nature of test functions, you will be best off just reading the source rather than leaning heavily on the descriptions generated by rustdoc. (The descriptions are still there, but they’re much less useful than they have been in previous episodes.) In particular, the test
module here is excluded because of the use of the #[cfg(test)]
attribute marker on it.
Because we are using the feature-gated benchmarking functionality, the show notes “library” can now only be compiled with the Rust nightly (as of 1.5, the version current as this episode is produced).
One thing that isn’t necessarily obvious from reading the test documentation in the Rust book and Rust reference: the extern crate test
statement needs to be not in this module, but at the module (lib.rs
) which defines the library/crate; in this case, show_notes/lib.rs
.
Legacy Code
Software developers spend a large part of our careers dealing with legacy code. But what is the best way to deal with legacy code? When should you rip out the old and rewrite it, and when should you opt for smaller clean-up jobs because, however ugly, what is already present works?
Polyglot Programming Promises and Perils
Sometimes, you’re doing a technical interview, and you just cannot figure out why your JavaScript function isn’t behaving like it should… and then, prompted by the interviewer, you realize that you’re not returning anything. Polyglot programming is beautiful, and wonderful, and sometimes it bites you when you aren’t looking.
Today, we are talking about modules, packages, and APIs in Rust. Taking a bit of a breather after some pretty hard material the last few weeks.
For reference, the Rust book section on Crates and Modules will be very helpful.
I accidentally called this episode 5, instead of episode 6. Whoops.
Just before the 15:00 mark, while discussing libraries, I referred to “e006.md” when I meant to say “e006.rs”. Slips of the tongue inspired by the fact that Rust (delightfully) uses Markdown for its documentation.
Allocate it where?
Subject: Returning functions from other functions, and thinking about the stack, the heap, and reference types.
This episode, we look at returning functions from other functions, and as part of that discuss some basics about the stack and the heap—and why we need to care about them for returning functions.
The functions themselves are not especially interesting; they just show you the basic form you use to return functions from other functions, and how to then use them in another function. You’ll want to take a detailed look instead at the documentation for each (or just read the source!), because that’s where the meat of the discussion in this week’s code is.
Subject: Functions, methods, closures, and function as arguments!
This week’s episode covers the basics of all sorts of functions: normal functions, methods, and closures.
No More Nulls
Subject: Enumerated (enum
) types, pattern matching, and meaningful return types.
Today’s episode discusses, in order:
enum
s in C and in Rust.switch
blocks in C-like languages.Option
and Result
enumerated types with pattern matching to provide meaningful returns from functions safely.There is a specific order to the examples below, and it is not the automatically-alphabetized order rendered by rustdoc
. Instead, you should work through in the sequence they appear in the source:
Something borrowed, something… moved?
Subject: The struct
data type constructor, and the basics of Rust’s “ownership” concept and “borrowing” and “moving”.
Today’s episode discusses, and the associated source code demonstrates, a few basic behaviors of structs… including borrowing!
After taking a short look at one of Rust’s basic approaches to creating new types, we dive into a fairly thorough overview of how borrowing works in fairly run-of-the-mill Rust code. This is a basic introduction, and as such I’m not getting into things like heap-allocated memory (Box
) or dealing with move
semantics with threads or closures. (I haven’t actually figured those out well enough yet to write something like this for them!)
As usual, you’ll want to have the src
open to see what I’m doing with the components documented below.
rustfmt
– a tool for formatting Rust code
Subject: Documentation in general, and rustdoc
and cargo doc
in particular.
This is a mostly-empty module, and it is intended as such. Why? Well, because almost all the sample code exists in these comments, which serve as the show notes. If you listen to the episode or take a look at the source files, you’ll see how it works!
The components below are included solely so you can see how the docstrings work with each kind of thing. Make sure to click on the names of the items: there is more documentation there. Again, take a look at the source to see how it looks in the context of a file module.
Note that this module-level docstring uses rather than `///`-style comments. This is because this docstring is documenting the item which contains it, rather than the following item. Per [Rust RFC 505][1], the preferred approach is always to use the "following" form (`///`) rather than the "containing" form (
), except for module-level docs like these. (I will be following RFC 505 throughout.)
rustdoc
Subject: The host, the language, and the show!
Today’s show is pretty meta. You can skip it if you just want to start with something more technical, but I thought listeners might want to know a little about the origins of the show and my own background, so that’s what you get today. Next time, we’ll be tackling the rustdoc
command in some detail.
This is an almost-empty module: we aren’t doing any fun code samples yet. I included the standard “Hello, world!” example, because how could I not? However, at some point in the future, there will be much more detailed code samples available:
Hopefully, the result will be a pretty helpful bunch of side content along with the audio of the podcast itself.
En liten tjänst av I'm With Friends. Finns även på engelska.