Skip to content

What is 6th unary operator? #20876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
khiri-co opened this issue Jan 10, 2015 · 3 comments · Fixed by #21014
Closed

What is 6th unary operator? #20876

khiri-co opened this issue Jan 10, 2015 · 3 comments · Fixed by #21014

Comments

@khiri-co
Copy link

In reference 7.2.11 Unary operator expressions

Rust defines six symbolic unary operators.

But in document there is only 5 ops; '-', '*', '!', 'box', '&'.
What is the 6th unary op?

By the way, in Rust '+' is not an unary op. So this expression is invalid; 'let a = +(1+2);'.
Considering the symmetry of "+" & '-', I think '+' may be an unary operator.
Of course, '+' unary op has no effect.

@nagisa
Copy link
Member

nagisa commented Jan 10, 2015

.. is unary operator in some contexts.

In general reference is somewhat out-of-date, so there might actually be any number of operators not mentioned in reference and introduced recently.

@japaric
Copy link
Member

japaric commented Jan 10, 2015

TL;DR Probably the deprecated ~ operator, that was used to box things. ~"foo" created a Box<str>, where as ~[1, 2] created a Box<[T]>.

According to the compiler internals, we have 4 unary operators (in the AST):

(Operator name : expression)

  • UnUniq: ~"string" (which is deprecated syntax)
  • UnDeref: *boxed_thing
  • UnNot: !bits
  • UnNeg: -num

From the POV of the compiler, the feature gated box thing is an expression. And references &foo/&mut bar are also expressions.

You may think of & as the "address of" operator, and box as the "box" operator. But that's not quite right, because when you use them in patterns like let &e = &(2, 3), they don't perform any operation at all.

@japaric
Copy link
Member

japaric commented Jan 10, 2015

(Oh and the reference is probably out of date in several places, we haven't put effort in updating it yet)

steveklabnik added a commit to steveklabnik/rust that referenced this issue Jan 12, 2015
alexcrichton added a commit to alexcrichton/rust that referenced this issue Jan 15, 2015
dlrobertson pushed a commit to dlrobertson/rust that referenced this issue Nov 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants