Crust of Rust: Subtyping and Variance

Crust of Rust: Subtyping and Variance

In this episode of Crust of Rust, we go over subtyping and variance — a niche part of Rust that most people don't have to think about, but which is deeply ingrained in some of Rust's borrow ergonomics, and occasionally manifests in confusing ways. In particular, we explore how trying to implement the relatively straightforward `strtok` function from C/C++ in Rust quickly lands us in a place where the function is more or less impossible to call due to variance!

In this episode of Crust of Rust, we go over subtyping and variance — a niche part of Rust that most people don't have to think about, but which is deeply ingrained in some of Rust's borrow ergonomics, and occasionally manifests in confusing ways. In particular, we explore how trying to implement the relatively straightforward strtok function from C/C++ in Rust quickly lands us in a place where the function is more or less impossible to call due to variance!

  • 0:00:00 Introduction
  • 0:02:30 Practical variance in strtok
  • 0:07:41 A simple strtok test
  • 0:09:45 Implementing strtok
  • 0:13:00 Why can't we call strtok?
  • 0:17:26 Pretending to be the compiler
  • 0:19:03 Shortening lifetimes
  • 0:25:40 Subtypes
  • 0:29:12 Covariance
  • 0:33:15 Contravariance
  • 0:42:14 Invariance
  • 0:50:00 &'a mut T covariance in 'a
  • 0:57:57 What went wrong in our strtok test?
  • 1:02:24 Fixing strtok
  • 1:07:34 Why is 'b: 'a not needed?
  • 1:09:08 Shortening &'a mut and NLL
  • 1:10:11 Is 'b: 'a implied for &'a &'b?
  • 1:12:54 Variance, PhantomData, and drop check
  • 1:28:06 Reasons for changing variance
  • 1:30:47 for{'a} and variance
  • 1:31:51 Mutating through *const T
  • 1:33:29 NonNull{T}
  • 1:35:26 How we got here

rust programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Developer Career Path: To Become a Team Lead or Stay a Developer?

For a developer, becoming a team leader can be a trap or open up opportunities for creating software. Two years ago, when I was a developer, ... by Oleg Sklyarov, Fullstack Developer at Skyeng company

Schedule the Program in Rust

In this Rust tutorial, we will discuss how we can schedule any Rust program by which it can execut. Rust is a multi-paradigm system programming language that can provide better memory safety while maintaining high performance. Rust also comes with a fairly nice build system (cargo) that keeps your libraries up to date or locks you to a specific version.

Tracking a Developer’s Journey From Documentation Visit

Measuring website activity provides only half the story. See how to best track the developer's journey and what funnel stages makes sense for API-first products

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.