← SnapRecaps

In Search of Code Purity

► 115,505 views ⏲ 16:37 Watch on YouTube ↗

Summary

Rust offers practical purity through const functions and compiler guidance, balancing Haskell-like safety with Lisp-like flexibility, making it a pragmatic evolution beyond C++ for everyday development.

Executive Summary

Formal methods and Haskell offer strong safety guarantees but are too expensive and complex for everyday web development, so the best compromise is ML-inspired languages like Rust. Tris argues Rust is not merely "C++ but better," but rather a hybrid of Haskell's purity and Lisp's flexibility hidden inside a C-like language, allowing it to sneak into production. Rust achieves practical purity through const functions, whose output depends only on inputs, enabling caching, parallelization, and predictable behavior—though with restrictions similar to Haskell's IO, excluding file access, printing, and mutable references. Clippy and the compiler actively guide developers, such as flagging when a function can be made const, showing that side-effect safety is encoded in the type system without a formal purity system. The core thesis, "Rust is as pure as possible, but no purer," sums up this pragmatic balance between soundness and usability.

Key Points

  • ▶ 0:00 Formal methods provide strong safety guarantees for critical systems, but they are expensive, require unusual verification languages, and slow down iteration — making them impractical for everyday web development.

  • ▶ 1:54 Haskell's functional purity, enforced via type signatures without IO, helps manage side effects and eliminates about half of the "nightmare errors" in Tris's experience.

  • ▶ 2:35 Like formal methods, Haskell is too complicated for general programming, so the best compromise is ML-inspired languages that offer compile-time type safety, functional programming, and side-effect elimination.

  • ▶ 5:09 After years with Scala, Haskell, Clojure, Nim, and Go, he found Rust in 2020; its ML roots show through OCaml's influence on the language's design.

  • ▶ 5:25 Rust is not "C++ but better" — it's "Haskell standing on Lisp’s shoulders, hiding in C’s coat to sneak into production."

  • ▶ 6:10 For Rust tooling, enable all lints and run Clippy in a terminal; the first error is the one you must fix.

  • ▶ 6:26 All code examples now live in a test module compiled into main.rs, enabling static type-checking for literate programming; the test prelude and fixture attribute configure pretty errors.
  • ▶ 7:05 Rust supports “pure functions, kind of,” whose output depends only on inputs and has no side effects—guaranteeing same-result calls, enabling caching, easier debugging, parallelization, and dead-code elimination.
  • ▶ 8:48 Clippy flags that a simple function could be a const function, and the fix is to “obey the compiler” by making it const—showing Rust’s type system already encodes side effects without a formal purity system.
  • ▶ 9:18 Const functions can execute at both compile time and runtime, but only use a limited subset of Rust—these restrictions are what make them safe and predictable.
  • ▶ 10:37 Const purity excludes mutable references, interior mutability, most floating-point operations (due to processor-dependent side effects), file system access, and printing—making them similar to Haskell IO restrictions.
  • ▶ 13:32 Though you cannot read environment variables directly in const functions, the macro version (std::env!) is allowed—macros act as the escape hatch for strict const rules.
  • ▶ 15:49 Rust achieves soundness of parallel code without a restrictive gated purity system, embodying "practical" purity.
  • ▶ 16:02 The core thesis: "Rust is as pure as possible, but no purer" — a pragmatic balance between theory and usability.
  • ▶ 16:06 Thanks viewers, directs support to Patreon, and promotes podcasts, GitHub links, and Discord for further discussion.

Video Sections

  • ▶ 0:00 Introduction, Formal Methods, and Haskell (0:00 - 3:15) - - Introduces the channel, his formal methods background, the appeal of Haskell, and Patreon support.
  • ▶ 3:15 The Functional Programming Journey to Rust (3:15 - 6:28) - - Covers Clojure, Nim, Go, discovering Rust, and initial Rust project/tooling setup.
  • ▶ 6:28 Rust Setup, Pure Functions, and Clippy (6:28 - 9:22) - - Demonstrates test/literate setup, functional-lite features, benefits of pure functions, and Clippy flagging a const function.
  • ▶ 9:22 Const Functions: Purity in Practice (9:22 - 15:51) - - Explains what const functions can and cannot do, side effects, debugging, macro escapes, and Rust's granular purity model.
  • ▶ 15:51 Conclusion, Outro, and Closing Promo (15:51 - 16:38) - - Wraps up why Rust is practically pure, thanks supporters, and promotes a podcast.

Exact Transcript

Load the full timestamped transcript on demand and click any time to jump in the video.