Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binary operation `|` cannot be applied to type

Tags:

rust

I have this error

error: binary operation | cannot be applied to type &mut u16

With this code,

fn f_op(op: &mut u16) {

    let mut addr: u16 = (op | 0xFFF);
    ..//
}

Solve it change &mut to &

fn f_op(op: & u16) {

But I wonder why I can not use | with &mut, sure there exists a good reason, but someone can explain to me.

Play-Rust

like image 836
Angel Angel Avatar asked Dec 24 '22 06:12

Angel Angel


2 Answers

Because the BitOr trait is not implemented for &mut u16.. however, it is for &u16.

BitOr trait

As viraptor points out, you could also dereference it with:

*op | 0xFFF;

.. which would make it a u16.. which also implements the BitOr trait as you can see above.

like image 196
Simon Whitehead Avatar answered Dec 31 '22 02:12

Simon Whitehead


You probably want to or the value, not the reference.

let mut addr: u16 = *op | 0xFFF;
like image 32
viraptor Avatar answered Dec 31 '22 02:12

viraptor