operator

Hunter_Joe·2023년 7월 31일
0

Rust 기초

목록 보기
2/8
post-thumbnail

operator

-컴파일러에게 작업 수행을 지시

type of operators

1. unary operators

  • 단일 피연산자에 작용하는 연산자

    types

    • borrow expression

      &, &mut

    • dereference expression

      *

    • negation expression
    • logical negation expression

      !

2. binary operators

  • 두개의 피연산자를 다루는 연산자

types

  • Arithmetic Expression

    +, -, *, /, %

  • Logical Expression

    &&, ||

  • Comparison Expression

    >,<,<=,>=,==,!=

  • Bitwise Expressions

    &,|,^

  • Assignment Expression

    =

  • Compound Assignment Expression

    -=,+=,/=,%=,*=

  • Typecast Expression

    as


arithmetic operators

  • 산술 연산자 : 산술 연산을 수행에 사용
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);
}

logical operators

  • 논리 연산자 : true/false 값에 작동

note
The logical AND and OR 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);
}

comparison operators

  • 비교 연산자 : 두 피연산자의 값을 비교
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);
}

bitwise operators

  • 비트 연산자 : 피연산자의 이진 표현을 처리

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);
}

compound assignment operator

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);
}

type casting operator

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.

borrowing operators

  • borrowing : reference the original data binding or to share the data.

    References are just like pointers in C.

  • Two variables are involved in a borrowing relationship when the referenced variable holds a value that the referencing variable borrows. The referencing variable simply points to the memory location of the referenced variable.

1. shared borrowing

  • 단일 또는 다수가 공유하지만 변경할 수 없는 데이터 조각

2. mutable borrowing

  • 단일 변수에 의해 공유되고 변경되는 데이터 조각
    (그러나 해당 데이터는 해당 시점에 다른 변수에 액세스할 수 없음)

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
}

dereferencing operator

역참조 연산자 : 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.

operatoroperationexplanation
*operand 1 = operand2dereference a valuepoint 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
}
profile
hunting season

0개의 댓글