What Rust borrowed by Emil Hernvall

December 9, 2020

In the presentation What Rust Borrowed from Emil Hernvall, engineer Emil at Amazon Web Services, discusses how Rust, a systems programming language, has adopted concepts from functional programming, making it an appealing choice for functional programmers. Rust’s first-class functions, algebraic data types, and emphasis on immutability and a powerful type system have functional programming roots. The presentation then demonstrates how Rust’s borrow checker and functional programming principles encourage safe coding through examples like handling data variance with pattern matching and using the some type. The speaker also discusses the implementation of a drawing API and a parser for S-expressions in Rust, highlighting the language’s method attachment, pattern matching, and use of traits for more flexibility. The video concludes with a discussion on the power of Rust’s traits and the influence of functional programming languages like Haskell on Rust’s functional approach.

What Rust borrowed: A comprehensive overview

Functional Programming in Rust

Emil Hernvall opens his presentation by discussing how Rust, although not purely functional, borrows heavily from functional programming (FP). Functional programming emphasizes the use of pure functions, immutability, and the composition of functions, all of which are well-supported in Rust. Rust’s powerful type system, first-class functions, and algebraic data types align with the FP paradigm, making it easier for developers to write safe and expressive code.

He notes that Rust’s encouragement of immutability and its high level of type inference make it a natural fit for developers familiar with FP. While Rust does not enforce functional programming as strictly as languages like Haskell, it provides the tools and features that allow developers to adopt a functional style effectively.

 

Borrow checker and safe Functional Programming

A significant aspect of Rust that Emil highlights is the borrow checker, which enforces safe memory access patterns, reducing the likelihood of bugs related to data mutation. This concept, while not directly from functional programming, complements FP’s emphasis on immutability and safe data handling.

Emil explains how Rust’s pattern matching and algebraic data types facilitate safe handling of data variance — a key element in FP. For example, Rust’s Option type, akin to the Maybe type in Haskell, allows developers to handle cases that could otherwise result in null pointer exceptions, a common source of errors in imperative languages.

 

Implementing functional concepts in Rust

Emil delves into practical examples to demonstrate how Rust enables functional programming. He discusses the implementation of a drawing API, where methods like draw_area leverage Rust’s pattern matching and method attachment capabilities. This approach not only simplifies the code but also aligns with the FP principle of small, composable functions.

He also introduces a domain-specific language (DSL) for flag definitions, implemented using S-expressions. The parser for this DSL utilizes Rust’s iterator pattern and Option type, demonstrating how Rust’s standard library supports functional programming idioms like error handling through options rather than exceptions.

 

Traits and functional abstractions

One of Rust’s most powerful features, according to Emil, is its trait system. Traits in Rust offer more flexibility than interfaces in languages like Java. Emil shows how traits can be used to abstract the drawing process, allowing for greater code reuse and flexibility — another hallmark of functional programming.

He draws parallels between Rust traits and Haskell type classes, underscoring how Rust’s design is influenced by functional programming languages. Emil cautions against making invalid states representable, advocating for careful design to maintain code safety and integrity, a principle that resonates deeply with FP.

 

Rust’s functional style vs. other languages

Emil reflects on Rust’s functional programming style in comparison to other languages, particularly Haskell and Java. While Java has incorporated some FP features in recent years, he argues that Rust’s approach is more natural and typical of a language designed with functional programming in mind from the outset.

He also shares his personal journey from working with more traditional languages to embracing functional programming, which he notes has become more mainstream in modern software development. This perspective is particularly valuable for developers transitioning from object-oriented or imperative languages to Rust.

 

Conclusion: Rust as a functional programming ally

Emil Hernvall’s talk provides a comprehensive look at how Rust integrates functional programming concepts, making it a powerful tool for developers who value safety, expressiveness, and immutability. While Rust may not be a purely functional language, its features — like the borrow checker, pattern matching, and traits— borrow heavily from the FP paradigm, enabling developers to write more reliable and maintainable code.

For those with a functional programming background or those looking to adopt a functional style in their systems programming, Rust offers a compelling blend of performance, safety, and expressive power. Hernvall’s insights highlight why Rust is increasingly seen as an ally for functional programmers venturing into systems-level programming.

 

Additional resources

Check out more from the MeetUp Func Prog Sweden. Func Prog Sweden is the community for anyone interested in functional programming. At the MeetUps the community explore different functional languages like Erlang, Elixir, Haskell, Scala, Clojure, OCaml, F# and more.