53

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 - Krabbe mit Hüten
  • 24,143
  • 12
  • 85
  • 121
Verax
  • 2,187
  • 5
  • 24
  • 39

1 Answers1

62

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
  • 326,504
  • 69
  • 892
  • 1,159
Lukas Kalbertodt
  • 66,297
  • 20
  • 206
  • 264
  • 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