-컴파일러에게 작업 수행을 지시
단일 피연산자에 작용하는 연산자
- borrow expression
&
,&mut
- dereference expression
*
- negation expression
- logical negation expression
!
- Arithmetic Expression
+
,-
,*
,/
,%
- Logical Expression
&&
,||
- Comparison Expression
>
,<
,<=
,>=
,==
,!=
- Bitwise Expressions
&
,|
,^
- Assignment Expression
=
- Compound Assignment Expression
-=
,+=
,/=
,%=
,*=
- Typecast Expression
as
fn main() {
let a = 4;
let b = 3;
println!("Operand 1:{}, Operand 2:{}", a , b);
println!("Add:{}", a + b);
println!("Sub:{}", a - b);
println!("Mul:{}", a * b);
println!("Div:{}", a / b);
println!("Modulus:{}", a % b);
}
true/false
값에 작동note
The logicalAND
andOR
are known as Lazy Boolean expressions because the left-hand side operand of the operator is first evaluated. If it is false, there is no need to evaluate the right-hand side operand in case of AND. If it is true, there is no need to evaluate the right-hand side operand in case of OR.
fn main() {
let a = true;
let b = false;
println!("Operand 1:{}, Operand 2:{}", a , b);
println!("AND:{}", a && b);
println!("OR:{}", a || b);
println!("NOT:{}", ! a);
}
fn main() {
let a = 2;
let b = 3;
println!("a > b:{}", a > b);
println!("a < b:{}", a < b);
println!("a >= b:{}", a >= b);
println!("a <= b:{}", a <= b);
println!("a == b:{}", a == b);
println!("a != b:{}", a != b);
}
fn main() {
let a = 5;
let b = 6;
println!("Operand 1: {}, Operand 2: {}", a , b);
println!("AND: {}", a & b);
println!("OR: {}", a | b);
println!("XOR: {}", a ^ b);
println!("NOT a: {}", !a);
println!("Left shift: {}", a << 2);
println!("Right shift: {}", a >> 1);
}
fn main() {
//define a mutable variable
let mut a = 2;
println!("a:{}", a);
a += 1;
println!("a+=1:{}", a);
println!("a:{}", a);
a -= 1;
println!("a-=1:{}", a);
println!("a:{}", a);
a /= 1;
println!("a/=1:{}", a);
println!("a:{}", a);
a *= 3;
println!("a*=3:{}", a);
}
casting : 타입 변환 as
keyword 사용
fn main() {
let a = 15;
let b = (a as f64) / 2.0;
println!("a: {}", a);
println!("b: {}", b);
}
note
📝What data types can be type casted?
- integer can be type casted to floating-point and vice versa.
- Integer can be typecasted to String
📝What data types cannot be type casted?
- String (&str) or character cannot be type casted to the data type of type integer or float.
- Character cannot be type casted to String type and vice versa.
References are just like pointers in C.
note
- mutable references(mutable borrow operations)는 이동된다.
- immutable references(shared borrow operations)는 복사된다.
fn main() {
let x = 10;
let mut y = 13;
//immutable reference to a variable
let a = &x;
println!("Value of a:{}", a);
println!("Value of x:{}", x); // x value remains the same since it is immutably borrowed
//mutable reference to a variable
let b = &mut y;
println!("Value of b:{}", b);
*b = 11; // derefencing
println!("Value of b:{}", b); // updated value of b
println!("Value of y:{}", y); // y value can be changed as it is mutuably borrowed
}
역참조 연산자 : Once you have a mutable reference to a variable, dereferencing is the term used to refer to changing the value of the referenced variable using its address stored in the referring variable.
operator | operation | explanation |
---|---|---|
*operand 1 = operand2 | dereference a value | point to the value of a mutable borrow variable 그리고 변수의 값을 변경할 수 있다. |
fn main() {
//mutable reference to a variable
let mut x = 10;
println!("Value of x:{}", x);
let a = & mut x;
println!("Value of a:{}", a);
//dereference a variable
*a = 11;
println!("Value of a:{}", a);
println!("Value of x:{}", x); // Note that value of x is updated
}