Alphabetic logical operators

Hi Rustaceans!

I'm just discovering Rust, and liking what I see so far!

Quick question: would it possible to define

  • and to act like &&
  • or to act like ||

For example using a macro?

Would make the transition smoother for Python programmers!

Only inside a macro (by converting occurences of and and or with && and || respectively), but I advise against doing that. It just adds more complexity than it's worth.

2 Likes

Do you think so? There are way more important and deeper differences between Python and Rust. How an operator is spelled is perhaps one of the least of any pain points related to learning a new language. The syntactical part is easy – operators, function names, indentation conventions, you name it. There's usually an established standard for them that you don't have to question (or understand).

Much more pain is caused by not yet being able to think differently in the new language, since you don't learn a new language by transliterating code from another one.

Rust for example calls out many instances of sloppiness that Python lets you get away with. Another example is the use of algebraic types in lieu of inheritance. The "everything is a value" model of ownership, and the RWLock pattern of borrowing. A predominantly functional style around collections. And so forth.

Don't worry about what logical operators are spelled. Yes, I think it would have been better for Rust to choose keywords over symbols, as it is often the case. But they are now baked into the language spec, and they are relatively unimportant enough to stay this way.

12 Likes

Thanks for the info.

I would have preferred and/or over the symbols, because both symbols are already used elsewhere:

  • The & symbol is already used to denote a reference.
  • The double pipe || is already used for closures without arguments, which really confused me the first time I saw it.
1 Like

Definitely there are (much) deeper differences. It is just one stumbling block. Also, the symbols seem to have multiple meanings as I said in my reply to Phlopsi.

You definitely don't need to convince me of that :slight_smile:

1 Like

I guess something like this would be a start:

trait LogicKeywords {
    fn and(self, other: bool) -> bool;
    fn  or(self, other: bool) -> bool;
}

impl LogicKeywords for bool {
    fn and(self, other: bool) -> bool { self && other }
    fn  or(self, other: bool) -> bool { self || other }
}

Which would then be used as x.and(y) , x.or(y).

However, this would not have the same behavior in the sense that the second argument is evaluated only when necessary in the case of symbols. Is there a way to make other "lazy"?

other would have to be something like impl FnOnce() -> bool and then used like x.and(|| y).

At that point && is way easier than .and(||. Rust just isn't designed to have that part of syntax customizable.

There's a lot of memory management and strict typing in Rust that could be a real barrier for Python programmers. A keyword or two isn't going to help.

4 Likes