60

Looking at the list of bitwise operators in the Rust Book, I don't see a NOT operator (like ~ in C). Is there no NOT operator in Rust?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Verax
  • 2,409
  • 5
  • 27
  • 42

1 Answers1

76

The ! operator is implemented for many primitive types and it's equivalent to the ~ operator in C. See this example (playground):

let x = 0b10101010u8;
let y = !x;
println!("x: {:0>8b}", x);
println!("y: {:0>8b}", y);

Outputs:

x: 10101010
y: 01010101

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
  • 3
    For the record, now this can be found in the reference: https://doc.rust-lang.org/reference/expressions.html#negation-operators – jp48 Sep 08 '17 at 15:09
  • 3
    Now at https://doc.rust-lang.org/reference/expressions/operator-expr.html#negation-operators – Craig McQueen Aug 25 '20 at 03:59