Rust

기초 개념 익히기

구루싸 2024. 3. 21. 18:20
반응형
SMALL

요즘 핫한 프로그래밍 언어 Rust! 에 대해 알아보겠습니다. 

우선 당연히 설치부터 시작해야겠죠?

Installation

# Linux or macOS
$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh

# macOS
$ xcode-select --install

# Linux users should generally install GCC or Clang, 
# according to their distribution’s documentation. 
# For example, if you use Ubuntu, you can install the build-essential package.

Window 환경이라면, 아래로 가서 설치 진행하시면 되겠습니다.

 

Install Rust

A language empowering everyone to build reliable and efficient software.

www.rust-lang.org

설치가 완료되면, Rust의 build system 이자 package manager 인 cargo를 사용할 수 있게 됩니다. 

이제 기본 개념부터 살펴보겠습니다.

Common Programming Concepts

Variables and Mutability

fn main() {
    let x = 5;
    println!("The value of x is: {x}");
    x = 6; // error[E0384]: cannot assign twice to immutable variable `x`
    println!("The value of x is: {x}");
}

fn main() {
    let mut x = 5; # mut는 해당 변수가 mutable 하다는 것을 알려줌
    println!("The value of x is: {x}");
    x = 6;
    println!("The value of x is: {x}");
}

Data Type

Rust is a statically typed language,
which means that it must know the types of all variables at compile time

 

Scalar Type

 

◆ Integer Types

Length Signed Unsigned
8-bit i8 u8
16-bit i16 u16
32-bit i32 u32
64-bit i64 u64
128-bit i128 u128
arch isize usize

 

◆ Integer Literals

Decimal 98_222
Hex 0xff
Octal 0o77
Binary 0b1111_0000
Byte (u8 only) b'A'

 

◆ 그 밖의 타입들

fn main() {
	// Floating-Point Types
    let x = 2.0; // f64
    let y: f32 = 3.0; // f32
    
    // The Boolean Type
    let t = true;
    let f: bool = false; // with explicit type annotation
    
    // The Character Type
    let c = 'z';
    let z: char = 'ℤ'; // with explicit type annotation
    let heart_eyed_cat = '😻';
}

 

Compound Types

Compound types can group multiple values into one type
fn main() {
	// The Tuple Type
    let tup: (i32, f64, u8) = (500, 6.4, 1);
    // let tup = (500, 6.4, 1);
    let (x, y, z) = tup;
    
    let a: (i32, f64, u8) = (500, 6.4, 1);
    let five_hundred = a.0;
    let six_point_four = a.1;
    let one = a.2;
    
    // The Array Type
    let array = [1, 2, 3, 4, 5];
    let first = array[0];
    let second = array[1];
    let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    let b: [i32; 5] = [1, 2, 3, 4, 5];
    let c = [3; 5]; // let c = [3, 3, 3, 3, 3]
}

Functions

fn five() -> i32 {
    5
}

fn main() {
    let x = five();

    println!("The value of x is: {x}");
    
    let y = {
        let x = 3;
        x + 1
    };

    println!("The value of y is: {y}");
}

Control Flow

fn main() {
	// if Expressions
    let number = 3;
    if number < 5 {
        println!("condition was true");
    } else {
        println!("condition was false");
    }
    let condition = true;
    let number = if condition { 5 } else { 6 }; // 5
    
    // Repetition with Loops
    let mut counter = 0;
    let result = loop {
        counter += 1;

        if counter == 10 {
            break counter * 2;
        }
    }; // counter = 20
    let mut count = 0;
    'counting_up: loop {
        println!("count = {count}");
        let mut remaining = 10;

        loop {
            println!("remaining = {remaining}");
            if remaining == 9 {
                break;
            }
            if count == 2 {
                break 'counting_up;
            }
            remaining -= 1;
        }

        count += 1;
    } // count is 2
    
    // Conditional Loops with while
    let a = [10, 20, 30, 40, 50];
    let mut index = 0;

    while index < 5 {
        println!("the value is: {}", a[index]);

        index += 1;
    }
    
    // Looping Through a Collection with for
    for element in a {
        println!("the value is: {element}");
    }
    for number in (1..4).rev() {
        println!("{number}!");
    }
    
}

Ownership, References, Borrowing, Slice Type 

Ownership is Rust’s most unique feature and has deep implications for the rest of the language. It enables Rust to make memory safety guarantees without needing a garbage collector, so it’s important to understand how ownership works

Ownership Rules

  • Each value in Rust has an owner.
  • There can only be one owner at a time.
  • When the owner goes out of scope, the value will be dropped.

 

 

fn main() {
    let a = String::from("aaaa");
    let size = calculate_length(&a);
    // let size = calculate_length(a); // occurs error, because ownership moves
    println!("{} {}", a, size);
	
    let mut s = String::from("hello");
    {
    	let r1 = &mut s;
        println!("{}", r1);
    } // r1 goes out of scope here, so we can make a new reference with no problems.

    let r2 = &mut s;
    println!("{}", r2);
    
    
    let r3 = &s; // no problem
    let r4 = &s; // no problem
    println!("{} {}", r3, r4); // no problem
    let r5 = &mut s; // no problem
    // println!("{}, {}, and {}", r3, r4, r5); // occurs error, because we also cannot have a mutable reference while we have an immutable one to the same value. 
    println!("{}", r5); //no problem
    
    let my_string = String::from("hello world");

    // `first_word` works on slices of `String`s, whether partial or whole
    let word = first_word(&my_string[0..6]);
    let word = first_word(&my_string[..]);
    // `first_word` also works on references to `String`s, which are equivalent
    // to whole slices of `String`s
    let word = first_word(&my_string);

    let my_string_literal = "hello world";

    // `first_word` works on slices of string literals, whether partial or whole
    let word = first_word(&my_string_literal[0..6]);
    let word = first_word(&my_string_literal[..]);

    // Because string literals *are* string slices already,
    // this works too, without the slice syntax!
    let word = first_word(my_string_literal);
}
// a reference borrowing
fn calculate_length(s: &String) -> usize {
    s.len()
}
fn dangle() -> &String { // dangle returns a reference to a String

    let s = String::from("hello"); // s is a new String

    &s // we return a reference to the String, s
} // Here, s goes out of scope, and is dropped. Its memory goes away.
  // Danger!

 

반응형
LIST