Search code examples
rustlanguage-design

What is the reason that the designers of Rust chose the symbols !/&&/|| instead of the words not/and/or?


What was the rationale used for choosing the operators !/&&/|| over not/and/or? Is there some history behind it, or was it just a preference thing?

Some ideas I've had:

  • Rust followed C / C++. However, in the case of for-each loops, the more English-like version of for x in X is used instead of C++'s for (x: X), so Rust didn't just blindly follow C / C++ in everything.

  • The symbols are shorter. The number of characters seems similar between the two.

  • The syntax had to be different due to a collision with type-specifications. There is also difference of a as u8 instead of (u8)a.

Since Rust is a new language, I would think that they could choose either symbols or words without issue. What is the reason Rust chose symbols over words? Why did it not choose both?


Solution

  • Back in 2006, Graydon Hoare started working on Rust as a small project. Hoare also worked on the Servo browser engine. His goal for Rust was that it might help with the development said browser engine. Since the engine (like almost all other browser engines) was written in C++ at that time, Rust was basically a C++ replacement for Hoare.

    So using the same logical operators as C++ seemed just natural.

    When the GitHub repository was created in 2010, && and || were already in the Rust language. See the lazy-and-or.rs test during the second commit in the repository:

    if (true && x) { ... }
    

    Over time, the language design process got a lot more democratic and a lot more ideas appeared. But it wasn't until 2016 that someone officially proposed adding not, and and or as an RFC.

    But as you can see the RFC was closed. The contributor withoutboats explained it like this:

    If I were designing a language from the beginning, I would probably prefer and and or. But we are generally opposed to contextual keywords unless absolutely necessary, contextual keywords in expression contexts are particularly difficult to introduce, and we tend to avoid syntactic sugar of the "pick your poison" variety.

    It is important that at that point !, && and || were already been stabilized for more than a year. So the only option was to add alternative operators, but not replace those ones. That didn't seem worth it.


    Disclaimer: I only joined the Rust community in 2014. There might have been larger discussions about this in IRC or other media I can't search. But this is all I could find.