All That We Love in Rust Was Stolen from OCaml !?

Volodymyr Pavlyshyn
5 min readSep 11, 2024

--

Rust has rapidly grown in popularity, becoming the go-to language for systems programming, known for its memory safety, speed, and concurrency. Developers admire Rust's modern language features, zero-cost abstractions, and tooling. However, the influence of Rust is partially original. One of the most interesting claims you might encounter is that “all that we love in Rust was stolen from OCaml.” This provocative statement highlights Rust’s roots in functional programming concepts, many of which are prominent in OCaml. This language may not be as popular but is loved by a niche group for its power and expressiveness.

Let’s break down what this means and explore the connections between Rust and OCaml, identifying where Rust has borrowed from OCaml and why it matters to Rust developers.

OCaml: The Silent Influence

OCaml is a multi-paradigm programming language, part of the ML (MetaLanguage) family, known for blending functional programming with imperative features. While it may not be as mainstream as Rust, OCaml has profoundly influenced the design of modern programming languages, especially those focused on type safety, pattern matching, and algebraic data types.

OCaml’s strengths include:
1. Strong static typing with type inference: OCaml has a robust type system that eliminates many common bugs during compilation rather than at runtime.
2. Algebraic data types and pattern matching: These features allow for clear and concise ways to model data, handle edge cases, and write expressive code.
3. Immutability and functional purity: OCaml strongly encourages a functional approach, which leads to code that is easier to reason about and maintain.
4. Concurrency and parallelism: While not historically as emphasized as in Rust, OCaml has evolved in terms of handling parallel and concurrent programming.

While many of these ideas exist in other languages, too, they are prominent in OCaml and can be seen mirrored in Rust’s design philosophy.

Borrowed Features: What Rust Loves and Where It Came From

Algebraic Data Types (ADTs) and Pattern Matching

One of the standout features of Rust is its powerful enum type, which closely resembles OCaml’s algebraic data types (ADTs). In Rust, enums allow developers to define types that can take on different shapes and forms, much like OCaml’s variants. This enables a high degree of expressiveness in Rust’s type system.

enum Result<T, E> {
Ok(T),
Err(E),
}

This is directly inspired by OCaml, where similar constructs make pattern matching and handling multiple cases of data straightforward and type-safe.

In OCaml:

type 'a result = 
| Ok of 'a
| Error of string

Both languages use pattern matching as a clean way to deconstruct these types, making code safer and more readable. Developers love Rust’s pattern matching because it encourages handling all cases explicitly, which helps eliminate runtime errors, but this is an inheritance from OCaml.

Ownership and Borrowing with a Functional Twist

While Rust is famous for its ownership model that guarantees memory safety without a garbage collector, the philosophy behind it isn’t purely new. As a functional language, OCaml emphasizes immutability and functional purity, which shares philosophical ground with Rust’s desire to prevent unintended mutations and side effects.

Rust’s borrowing and ownership mechanisms are essentially a stricter, more systems-focused version of functional programming’s emphasis on controlled state and data flow. Although OCaml doesn’t have the same concept of ownership and borrowing, it promotes the same goal of creating predictable, maintainable, and bug-free code.

Type Inference

Both Rust and OCaml rely heavily on type inference, which reduces boilerplate and makes code more concise. Rust’s type inference allows developers to omit many type annotations, making code easier to read and write. While this feature is not unique to OCaml, the ML family has championed it for decades.

In OCaml:

let add x y = x + y (* The compiler infers that x and y are integers *)

Similarly, in Rust:

let sum = |x, y| x + y; // The compiler infers the types of x and y

OCaml’s powerful type inference system is one of the things that attracted language designers to integrate similar functionality in Rust. Both languages offer the flexibility to skip explicit types while still maintaining type safety.

Concurrency and Safety

Concurrency is an area where Rust shines with its strict guarantees around safe, concurrent code. However, Rust’s interest in achieving safe concurrency also borrows inspiration from the functional programming world. In OCaml, immutability by default and higher-order functions reduce the likelihood of bugs in concurrent systems, as the data isn’t modified unexpectedly.

Rust pushes this further by enforcing strict rules with its ownership and borrowing model, but the underlying functional principle — minimizing mutable state — has long been part of the functional programming philosophy present in OCaml.

Community and Tools

Even how Rust’s community is built mirrors some of OCaml’s values. Both communities emphasize tooling and documentation, with Rust’s cargo hailed as one of the best build systems and package managers in the industry. While `cargo` isn’t a direct lift from OCaml, both communities appreciate the importance of having a cohesive set of tools that “just work.”

What Rust Innovates?

While the statement “Rust stole everything from OCaml” is a bit tongue-in-cheek, Rust brings many new ideas to the table, especially regarding systems programming. The **ownership and borrowing system** is an original Rust invention, and its integration of these concepts with performance and systems-level control is something OCaml never aimed to address.

Moreover, Rust’s focus on performance, memory safety without garbage collection, and real-time guarantees are where it diverges from OCaml. Rust’s primary target — low-level programming — means it had to innovate in areas like memory management, concurrency safety, and performance optimization, things that aren’t as critical in OCaml’s realm of high-level functional programming.

Conclusion

The influence of OCaml on Rust is undeniable. Many of the features Rust developers love — like algebraic data types, pattern matching, type inference, and functional purity — have strong roots in OCaml and the wider functional programming community. However, Rust takes these ideas and adapts them to the world of systems programming, creating a unique blend that combines the safety and expressiveness of functional languages with the performance and control needed for low-level programming.

So, while it’s fun to say “Rust stole everything from OCaml,” it’s perhaps more accurate to say that Rust borrowed and evolved some of OCaml’s best ideas, using them as a foundation to build a language that meets the needs of modern systems developers.

--

--

Volodymyr Pavlyshyn
Volodymyr Pavlyshyn

Written by Volodymyr Pavlyshyn

I believe in SSI, web5 web3 and democratized open data.I make all magic happens! dream & make ideas real, read poetry, write code, cook, do mate, and love.

No responses yet