Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this strange line do?

A pull request has been done with a new test for the Rust compiler. It verifies that a strange line can compile:

fn main() {
    let val = !((|(..):(_,_),__@_|__)((&*"\\",'@')/**/,{})=={&[..=..][..];})//
    ;
    assert!(!val);
}

What does this line do exactly?

like image 335
Boiethios Avatar asked Jun 06 '18 09:06

Boiethios


1 Answers

Let's break it down! First, I reformatted the line to somewhat increase "readability".

let val = !(
    (
        |(..): (_, _), __@_| __
    )(
        (
            &*"\\", 
            '@'
        ) /**/, 
        {}
    )
    ==
    {
        &[..=..][..];
    }
)//
;

It starts with let val = and ends with //<newline>;. So it's a simple let-binding of the form let val = ⟨v⟩;. Let's discuss what ⟨v⟩ does:

  • A negation via the not operator: !( ⟨_⟩ )
    • A comparison via ==: ⟨lhs⟩ == ⟨rhs⟩
      • ⟨lhs⟩: a function call of a closure ( ⟨closure⟩ )( ⟨args⟩ )
        • ⟨closure⟩: a closure definition |⟨first_param⟩, ⟨second_param⟩| ⟨body⟩
          • ⟨first_param⟩: (..): (_, _). This parameter has a type annotation (_, _) meaning that it is a 2-tuple. The pattern (where normally, you would find a single name) is (..) which means: a tuple, but ignore all elements of it.
          • ⟨second_param⟩: __@_. This is a pattern usually known from match bindings: name @ pattern. So the actual pattern is _ (which doesn't bind anything) and the value is bound via @ to the name __ (two underscores, which is a kind of valid identifier).
          • ⟨body⟩: __. This is just the identifier which we bound the second parameter to. So the closure is basically equivalent to |_, x| x.
        • ⟨args⟩: a list of two arguments with an inline comment /**/ in between: ⟨first_arg⟩/**/, ⟨second_arg⟩
          • ⟨first_arg⟩: (&*"\\", '@'). This is simply a 2-tuple where the first element is a string containing a backslash and the second is the char '@'. The &* for the first element cancels out.
          • ⟨second_arg⟩: {}. This is an empty block which has the type (). So as second argument, a unit is passed.
      • ⟨rhs⟩: a braced block with one statement inside: { ⟨stmt⟩; }. Note that this is a statement, with a semicolon. This means the result is not returned from the block. Instead the block returns () just as the empty block {}.
        • ⟨stmt⟩: an indexing expression { &⟨collection⟩[⟨index⟩] }.
          • ⟨collection⟩: [..=..]. This is an array with one element. The element is ..= .. which is a RangeToInclusive where end of the range is the RangeFull written ...
          • ⟨index⟩: ... This is just the RangeFull again.

So in summary: we compare the result of calling a closure to a braced block which evaluates to (). The closure is basically |_, x| x and the second argument we pass to it is {} (which evaluates to ()), so the whole closure calling expression evaluates to ().

This means the whole thing is equivalent to:

  • let val = !( () == () );, which is equivalent to:
  • let val = !( true );, which is equivalent to:
  • let val = false;
like image 200
Lukas Kalbertodt Avatar answered Oct 02 '22 02:10

Lukas Kalbertodt