Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Operadores de Atribuição

Usados para atribuir valores a variáveis.

+=

#![allow(unused)]
fn main() {
let mut num: u8 = 10;

// adição e atribuição: +=
num += 1;
println!("10 += 1 = {}", num);
}

-=

#![allow(unused)]
fn main() {
let mut num: u8 = 10;

// subtração e atribuição: -=
num -= 1;
println!("10 -= 1 = {}", num);
}

*=

#![allow(unused)]
fn main() {
let mut num: u8 = 10;

// multiplicação e atribuição: *=
num *= 2;
println!("10 *= 2 = {}", num);
}

/=

#![allow(unused)]
fn main() {
let mut num: u8 = 20;

// divisão e atribuição: /=
num /= 3;
println!("20 /= 3 = {}", num);
}

%=

#![allow(unused)]
fn main() {
let mut num: u8 = 6;

// resto e atribuição: %=
num %= 4;
println!("6 %= 4 = {}", num);
}