rust - deptno/deptno.github.io GitHub Wiki

rust

๊ณต๋ถ€

์ •๋ฆฌ ํ•„์š”

  • closure definition ์‹œ์ ์— borrow rule ์ด ๋™์ž‘ํ•œ๋‹ค
    • closure ๊ฐ€ mutable borrow ์ธ ๊ฒฝ์šฐ call ํ• ๋•Œ๊นŒ์ง€ ์ฝ”๋“œ์ƒ์œผ๋กœ print ๋ฅผ ์ฐ์ง€ ๋ชปํ•œ๋‹ค(borrow rule ์œ„๋ฐ˜)
  • workspace
    • Cargo.toml ์— ๋จผ์ € member ๋ฅผ ๋ช…์‹œํ•˜๊ณ  cargo new project_name --lib ์„ ์ถ”๊ฐ€
    • dependencies ์— relative path ๋กœ ์ถ”๊ฐ€
    • ๋‹ค๋ฅธ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ cargo run ์„ ํ•  ๊ฒฝ์šฐ -p project_name ์„ ์ถ”๊ฐ€ํ•ด์„œ ์‹คํ–‰

module|๋ชจ๋“ˆ

// https://doc.rust-lang.org/rust-by-example/mod/visibility.html
// ์ƒ์œ„ ๋ชจ๋“ˆ(parent or ancestor module) ์—๋งŒ ๋…ธ์ถœ
pub(in create::my_mod) fn private_function() {
    println!("private_function");
}
// == private fn
pub(self) fn function_name() {
    println!("function_name");
}
// parent
pub(super) fn function_name() {
    println!("function_name");
}
// crate
pub(crate) fn function_name() {
    println!("function_name");
}

๋ชจ๋“ˆ์˜ ๊ตฌ์กฐ๋Š” ํŒŒ์ผ๊ตฌ์กฐ์™€ ๊ฐ™๋‹ค. mod a; ์„ ์–ธ๋œ ๊ฒฝ์šฐ a.rs ํ˜น์€ a/mod.rs ํŒŒ์ผ์„ ์ฐธ์กฐํ•œ๋‹ค.

crete

์ปดํŒŒ์ผ ๋‹จ์œ„

์„ค์น˜

curl https://sh.rustup.rs -sSf | sh

ํˆด

rustup

rust update
rustup self uninstall
rustup docs --book

rustc

rustc --version
rustc main.rs

cargo

cargo ๋Š” ๋Ÿฌ์ŠคํŠธ์˜ ํŒจํ‚ค์ง€ ๋งค๋‹ˆ์ €

cargo --version
cargo new hello_cargo # git ๋„ ํ•จ๊ป˜ ์ดˆ๊ธฐํ™”๋œ๋‹ค.
cargo run # 
cargo build
cargo build --release
cargo check # ๋นŒ๋“œ๋ณด๋‹ค ๋น ๋ฅด๋‹ค, ์ปดํŒŒ์ผ ํ™•์ธ
cargo update # Cargo.lock ์„ ๋ฌด์‹œํ•˜๊ณ  miner ์—…๋ฐ์ดํŠธ, mager ์—…๋ฐ์ดํŠธ๋Š” ํŒŒ์ผ ์ž์ฒด๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.
name = "hello_cargo"
version = "0.1.0"
authors = ["deptno <[email protected]>"]
edition = "2018"

[dependencies]

rand = "0.6.1"

, toml ํฌ๋งท

์–ธ์–ด

cargo new ๋ฅผ ํ†ตํ•ด ํŒจํ‚ค์ง€๊ฐ€ ์ƒ์„ฑ๋˜๋ฉด src/{main,lib}.rs ํŒŒ์ผ์„ ํ†ตํ•ด ์‹คํ–‰ํŒŒ์ผ ํ˜น์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋นŒ๋“œ ์—”ํŠธ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค(๋‘˜๋‹ค ๊ฐ€๋Šฅ).

์†์„ฑ attribute

  • #![allow(dead_code)] - ์•ˆ์“ฐ๋Š” ์ฝ”๋“œ ๊ฒฝ๊ณ  ํ•˜์ง€ ์•Š์Œ
  • #![allow(unused)] - ์•ˆ์“ฐ๋Š” ๋ณ€์ˆ˜ ๊ฒฝ๊ณ  ํ•˜์ง€ ์•Š์Œ
  • #[allow(non_camel_case_types)] - ๊ฒฝ๊ณ  ์•ˆํ•จ

use

use std::io;

type|ํƒ€์ž…

  • primitive type
    • bool
    • char
    • integer
      • 2023-08-27 ๋ช…์‹œ์  overflow ํ•ธ๋“ค๋ง(release ๊ธฐ๋ณธ์€ wrapping_)์„ ์œ„ํ•ด ์•„๋ž˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
        • wrapping_* - maximum value + 1 == minimum value
        • checked* - Option
        • overflowing_* - (value, is_overflow)
        • saturating_* - ํ•œ๊ณ„๋ฅผ ๋„˜์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด maximum or minimum ๊ฐ’์„ ๋ฆฌํ„ด
    • float
    • unit - ํŠœํ”Œ์ด์ง€๋งŒ primitive type ์ด๋‹ค.
  • compound type
    • array
    • slice, array ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์ปดํŒŒ์ผํƒ€์ž„์— length ๋ฅผ ์•Œ ์ˆ˜ ์—†๋‹ค.
    • tuple
    • struct
    • enum

casting|์บ์ŠคํŒ…

  • i <-> u ์ •์ˆ˜๊ฐ„์˜ ๋ณ€ํ™˜์€ ํ•ด๋‹น ๊ฐ’์„ ํ‘œํ˜„ํ•˜๋Š” ํ˜•์‹์— ๋”ฐ๋ผ ๊ทธ๋Œ€๋กœ ์ ์šฉ๋œ๋‹ค.
  • floating <-> integer ๋ณ€ํ™˜์€ floating ๋ณ€์ˆ˜๊ฐ€ ํ‘œํ˜„ํ•˜๋Š” ๊ทผ์‚ฌ๊ฐ’์„ ๋‚ด๋ฆผํ•˜์—ฌ ์ •์ˆ˜ ๋ณ€ํ™˜ํ•œ๋‹ค. saturating cast

primitive ํƒ€์ž…์€ cast ๋ฅผ ํ†ตํ•ด์„œ ๋ณ€๊ฒฝ๋œ๋‹ค. custom ํƒ€์ž…์€ trait From ์ด๋‚˜ Into ๋ฅผ ํ†ตํ•ด์„œ ๋ณ€๊ฒฝ๋œ๋‹ค.

ํŠน์„ฑ|trait

  • Copy
    • copy trait ์ด ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ, ๋ณ€์ˆ˜ ๋Œ€์ž… ์ดํ›„์—๋„ ์œ ํšจํ•˜๋‹ค.
    • drop trait ์ด ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ, copy trait annotation ์„ ํ•  ์ˆ˜ ์—†๋‹ค.
    • copy ๊ฐ€๋Šฅํ•œ ํƒ€์ž…
      • ์ •์ˆ˜ํ˜• ํƒ€์ž…
      • ๋ถ€๋™ ์†Œ์ˆ˜์  ํƒ€์ž…
      • bool
      • char
      • copy ๊ฐ€๋Šฅํ•œ ํƒ€์ž…์œผ๋กœ๋งŒ ๋ฌถ์ธ ํŠœํ”Œ

ํ˜•๋ณ€ํ™˜

  • From
    • String::from("hello") ์™€ ๊ฐ™์ด from ์„ ๊ตฌํ˜„ํ•˜์—ฌ ํƒ€์ž… ๋ณ€ํ™˜์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค.
  • Into
    • From ์˜ ์ง์œผ๋กœ ํ•„์š”ํ•œ ์‹œ์ ์— From ์„ ํ˜ธ์ถœํ•˜๋„๋กํ•œ๋‹ค.
    • into ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ ํ•˜๋Š” ์ˆœ๊ฐ„ ๋Œ€์ž…ํ•  ๋ณ€์ˆ˜์˜ ํƒ€์ž…์— ๋งž์ถฐ์„œ from ์„ ์—ญ์œผ๋กœ ํ˜ธ์ถœํ•œ๋‹ค.
    let x: i32 = 5;
    let y: u32 = x.into(); // u32::from(x) ์™€ ๊ฐ™์€ ํ‘œํ˜„
  • TryFrom From ๊ณผ ๊ฐ™์œผ๋‚˜ ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฆฌํ„ด ํƒ€์ž…์€ Result<T, E> ์ด๋‹ค.
  • TryInto TryFrom ์˜ ์ง์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
    x.try_into()
  • ToString
    • std::fmt::Display ์„ ๊ตฌํ˜„ํ•˜๋ฉด to_string ํ˜ธ์ถœ์‹œ ์ด๋ฅผ ํ†ตํ•ด String ์œผ๋กœ ๋ณ€ํ™˜๋œ๋‹ค.
  • FromStr
    • parse ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์‹œ ํ•ด๋‹น ํƒ€์ž…์— ๊ตฌํ˜„๋˜์–ด์žˆ๋Š” FromStr ์„ ํ†ตํ•ด ๋ณ€ํ™˜๋œ๋‹ค.
    let parsed: i32 = "5".parse().unwrap();
    let turbo_parsed = "10".parse::<i32>().unwrap();

ownership|์†Œ์œ ๊ถŒ

& ์ฐธ์กฐ

์ฐธ์กฐ๋Š” ์†Œ์œ ๊ถŒ์„ ๊ฐ–์ง€ ์•Š๋Š”๋‹ค.

  • ๋ถˆํŽธ ์ฐธ์กฐ๋Š” ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ๊ฐ€๋ณ€ ์ฐธ์กฐ๋Š” ํ•˜๋‚˜๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ๋ถˆ๋ณ€ ์ฐธ์กฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ€๋ณ€ ์ฐธ์กฐ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. -> ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด ๋ถˆ๋ณ€ ์ฐธ์กฐ์˜ ๊ฐ’๋„ ๋ณ€๊ฒฝ๋˜์–ด ๋ฌผ๊ฒฐ์„ฑ์ด ๊นจ์ง€๊ธฐ ๋•Œ๋ฌธ

ํƒ€์ž… ๋ณ€ํ™˜

&String[..] === &str

impl

ref

& borrow ์™€ ๊ฐ™๋‹ค.

  let c = 'Q';
  let ref ref_c1 = c;
  let ref_c2 = &c;

destructure ๋ฅผ ํ†ตํ•ด์„œ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒฝ์šฐ์—๋„ ์†Œ์œ ๊ถŒ์€ ์ด๋™๋œ๋‹ค. ref ํ‚ค์›Œ๋“œ๋ฅผ prefix ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์†Œ์œ ๊ถŒ ์ด๋™์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค. stack value ์—์„œ๋Š” ํšจ๊ณผ๊ฐ€ ์—†๋‹ค.

variable|๋ณ€์ˆ˜

let x = 5; # ๋ถˆ๋ณ€
let x = 5.0; # ์ด์ „ ๋ณ€์ˆ˜๋Š” ๊ฐ€๋ ค์ง„๋‹ค(shadowing), ์ด์ „ ํƒ€์ž… ๋ฌด์‹œ ๊ฐ€๋Šฅ. ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ๋ณ€์ˆ˜๋Š” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ œ๊ฑฐํ•œ๋‹ค.

const CONSTANT: u32 = 100_000; # ํ•ญ์ƒ ํƒ€์ž… ์ง€์ •

let mut y = String::new(); # ๊ฐ€๋ณ€
y = 1; # ํƒ€์ž… ์—๋Ÿฌ

function|ํ•จ์ˆ˜

fn main() {
    println!("Hello, world!");
}
fn get_name() -> String;
pub fn get_name<T: AType>(a: T) -> T;
  • ์—ฐ๊ด€ ํ•จ์ˆ˜ associated : ํƒ€์ž…์— ์—ฐ๊ด€
  • ๋ฉ”์„œ๋“œ method : ์ธ์Šคํ„ด์Šค์— ์—ฐ๊ด€
    • sugar
      • stack : (&self) : (self: &Self)
      • heap : (self) : (self: Self) stack, heap ์—๋”ฐ๋ผ method ์˜ ์ธ์ž ํƒ€์ž…์ด ๋‹ฌ๋ผ์ง€๋Š”์ง€๋Š” ํ™•์ธํ•„์š” @todo

ํด๋กœ์ € closure

์ธ์ž๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฐธ์กฐ ๋นŒ๋ฆฌ๊ธฐ(& borrow) ์‹œ์ž‘ํ•˜์—ฌ ํ•„์š”์„ฑ์ด ์š”๊ตฌ๋  ๋•Œ๋งŒ ๋” ๋‚ฎ์€ ๋‹จ๊ณ„๋กœ ๊ฐ„๋‹ค. https://doc.rust-lang.org/rust-by-example/fn/closures/capture.html

non-copy ํƒ€์ž…์— ๋Œ€ํ•ด์„œ ํด๋กœ์ €๋ฅผ ์„ ์–ธํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ &๋กœ ์ฐธ์กฐ๋œ๋‹ค.
ํ•˜์ง€๋งŒ mutable ํ–‰๋™(std::mem::drop ๋“ฑ์„ ์‚ฌ์šฉ)์„ ์ทจํ•˜๋ฉด move ๋œ๋‹ค.
ํ˜น์€ ๋ช…์‹œ์ ์œผ๋กœ move ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

move ๋œ ์ดํ›„๋Š” ์†Œ์œ ๊ถŒ์ด ์ด๋™๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์บก์ณ๋œ ๋ณ€์ˆ˜๋ฅผ ๋”์ด์ƒ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

์ž…๋ ฅ ํŒŒ๋ผ๋ฉ”ํ„ฐ๋กœ์„œ์˜ ํด๋กœ์ € as input parameters

3๊ฐ€์ง€ trait ์ค‘ ํ•˜๋‚˜๋กœ ํ‘œํ˜„๋˜์–ด์•ผ ํ•œ๋‹ค.

  • Fn (&T) - capture by reference
  • FnMut (&mut T) - captures by mutable reference
  • FnOnce (T) - captures by value

FnOnce(via move) ๋Š” Fn, FnMut ์˜ superset ์ด๋ฏ€๋กœ ์–ธ์ œ๋‚˜ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ƒํ™ฉ์— ๋งž๋Š” ๊ฐ€์žฅ ํฐ ์ œ์•ฝ์„ ์ฃผ์–ด์•ผํ•œ๋‹ค.

๊ตฌ๋ฌธ

๊ฒฐ๊ด๊ฐ’์ด ์—†๋‹ค. ์„ธ๋ฏธ์ฝœ๋ก ์œผ๋กœ ๋๋‚œ๋‹ค.

ํ‘œํ˜„์‹

๊ฒฐ๊ด๊ฐ’์ด ์žˆ๊ณ  ๋•Œ๋ฌธ์— ๋Œ€์ž…์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์„ธ๋ฏธ์ฝœ๋ก ์œผ๋กœ ๋๋‚˜์ง€ ์•Š๋Š”๋‹ค. ํ•จ์ˆ˜ ๋ฆฌํ„ด์‹œ ์ฃผ์˜

  • if ๋Š” ์„ธ๋ฏธ์ฝœ๋ก ์œผ๋กœ ๋๋‚˜๋Š”๋“ฏ.
  • loop ๋„ break ๋ฌธ๊ณผ ํ•จ๊ป˜ ๊ฐ’ ๋ฆฌํ„ด์ด ๊ฐ€๋Šฅ

์ฃผ์„ comment

//

๋ฐ์ดํ„ฐ ํƒ€์ž…

์Šค์นผ๋ผ

  • integer {i,u}{8,16,32,64,size}
    • ๋ฆฌํ„ฐ๋Ÿด
      • decimal - 98_222
      • hex - 0xff
      • octal - 0o77
      • binary - 0b1111_0000
      • byte(u8) - b'A'
  • floating point numbers
    • f64 - default
    • f32
  • boolean
    • true
    • false
  • characters
    utf-8
    • 'z'
    • '๋ถˆ' - 3 bytes

์ปดํŒŒ์šด๋“œ ํƒ€์ž…

  • ํŠœํ”Œ
    • (i32, f64, u8) - ๋‹ค๋ฅธ ํƒ€์ž…๋„ ๊ฐ€๋Šฅ
    • a.0 ์œผ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • ๋ฐฐ์—ด
    • [i32; 10] - ๊ฐ™์€ ํƒ€์ž…๋งŒ ๊ฐ€๋Šฅ
    • [3; 5]
    • a[0] ์œผ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ
์ •์ˆ˜ ์˜ค๋ฒ„ํ”Œ๋กœ

ํƒ€์ž…์˜ ๊ฐ’์„ ๋„˜์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ Warpping ํƒ€์ž…์„ ํ†ตํ•ด 255 -> 0 ์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค. --release ๋นŒ๋“œ ์‹œ์—๋Š” ๋ช…์‹œ์ ์œผ๋กœ ์„ ์–ธํ•˜์ง€ ์•Š์œผ๋ฉด ์ž๋™์ ์œผ๋กœ ์„ ์–ธ๋œ๋‹ค.

์ฐธ

ํ”„๋ ๋ฅ˜๋“œ์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด use ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

๋ฐ˜๋ณต๋ฌธ

for i in 0..10 {
    println!("{}", i);
}
for i in (1..10).rev() {
    println!("{}", i);
loop {
    println!("loop");
    break;
}
while i < 10 {
    println!("{}", i);
    i += 1;
}

๋งคํฌ๋กœ

println!("hello, world!");

! ๊ฐ€ ๋ถ™์€ ํ•จ์ˆ˜๋Š” ๋งคํฌ๋กœ๋‹ค.

let foo = 5 // ๋ถˆ๋ณ€
let mut foo = 5 // ๊ฐ€๋ณ€
  • cfg!
  • print!
  • println!
  • eprint!
  • eprintln!

์—ฐ๊ด€ ํ•จ์ˆ˜, associated function, static method

String::new

new associated function ์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋ฉฐ ํƒ€ ์–ธ์–ด์˜ static method ์™€ ๊ฐ™๋‹ค.

std

use std::io;
let mut guess = String::new();
io:stdin().read_line(&mut guess).expect("Failed to read line");
let guess: u32 = guess.trim().parse().expect("Please type a number!");

use std::cmp::Ordering;
match guess.cmp(&secret_number) {
    Ordering::Less => println!("Too small!"),
    Ordering::Greater => println!("Too big!"),
    Ordering::Equal => println!("You win!"),
}

std::str

fn main() {
    let raw_str = r"Escapes don't work here: \x3F \u{211D}";
    println!("{}", raw_str);

    let quotes = r#"And then I said: "There is no escape!""#;
    println!("{}", quotes);

    let longer_delimiter = r###"A string with "# in it. And even "##!"###;
    println!("{}", longer_delimiter);
}

ํ† ํฐ์œผ๋กœ ์‚ฌ์šฉ๋  # ๊ฐฏ์ˆ˜๋Š” ์ œํ•œ์ด ์—†์Œ

std::collections:HashMap

std::collections:HashSet

HashMap<T, ()>

methods
  • union
  • difference
  • intersection
  • symmetric_difference - xor

std::rc::Rc

์—ฌ๋Ÿฌ ์†Œ์œ ๊ถŒ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋ฅผ ์œ„ํ•ด reference count ์‚ฌ์šฉ๋œ๋‹ค.

use std::rc::Rc;

fn main() {
    let rc_examples = "Rc examples".to_string();
    {
        let rc_a: Rc<String> = Rc::new(rc_examples);
        {
            let rc_b: Rc<String> = Rc::clone(&rc_a);
            // Two `Rc`s are equal if their inner values are equal
            println!("rc_a and rc_b are equal: {}", rc_a.eq(&rc_b));
            
            // We can use methods of a value directly
            println!("Length of the value inside rc_a: {}", rc_a.len());
            println!("Value of rc_b: {}", rc_b);
            println!("--- rc_b is dropped out of scope ---");
        }
    }
}

std::rc::Rc

atomic refernece counted,

std::sync::mpsc

multiple producer single consumer producer(tx) ๊ฐ€ ์—ฌ๋Ÿฟ์ธ ๊ฒƒ์€ ํ—ˆ์šฉ๋œ๋‹ค.

use std::sync::mpsc::{Sender, Receiver};

let (tx, rx): (Sender<i32>, Receiver<i32>) = mpsc::channel();
let thread_tx = tx.clone();
tx.send(1).unwrap(); // non-blocking send
rx.recv().unwrap(); // blocking recv

std::path::Path

  • posix::Path
  • windows::path ์ƒํ™ฉ์— ๋งž๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค. to_str ๋ฉ”์„œ๋“œ๋Š” utf-8 ์ด ์•„๋‹Œ๊ฒฝ์šฐ ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค(Option).

std::fs::File

std::process::{Command, Stdio}

std::Child

์‹คํ–‰์ค‘์ธ ์ž์‹ ํ”„๋กœ์„ธ์Šค์˜ ํ•ธ๋“ค์„ ๋…ธ์ถœํ•œ๋‹ค.

  • stdin
  • stdout
  • stderr
let process = match Command::new("wc")
                            .stdin(Stdio::piped())
                            .stdout(Stdio::piped())
                            .spawn() {
    Err(why) => panic!("couldn't spawn wc: {}", why),
    Ok(process) => process,
};

std::os::unix

std::env

  • env::args

foreign function interface : ffi

#[link(name = "mylib")]
extern {
  fn a(z: Complex) -> Complex;
}

C ๋ฐ”์ธ๋”ฉ์„ ์ง€์›, unsafe

๋งคํฌ๋กœ macro

println!("hello, world!");
println!("{}", "hello, world!");
println!("{} {}", "hello", "world");

enum

  • Result
    • method
      • expect(value: &str)
  • Ordering

?

FromResidual ์ด ๊ตฌํ˜„๋œ ํƒ€์ž…์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ, ์ œ๊ณต๋˜๋Š” ํƒ€์ž…์œผ๋กœ๋Š” ์•„๋ž˜๊ฐ€ ์žˆ์Œ

  • Result<T, E>
  • Option

ํ•จ์ˆ˜ ๊ฒฐ๊ด๊ฐ’์œผ๋กœ Error๊ฐ€ ๋ฆฌํ„ด๋˜๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜์—์„œ ๋ฆฌํ„ด ์ฒ˜๋ฆฌํ•œ๋‹ค.

dyn Trait, Trait Object

smart-pointer|์Šค๋งˆํŠธ ํฌ์ธํ„ฐ

  • Box: usize ๋งŒ ์Šคํƒ์— ๋‚จ๊ธฐ๊ณ  heap ์— ์ €์žฅํ•œ๋‹ค.
  • Rc: ๋‹ค์ค‘ ์†Œ์œ ๊ถŒ์„ ํ—ˆ์šฉํ•œ๋‹ค. Rc.clone()
    • Arc: Rc์˜ thread safe ๋ฒ„์ „
    • Rc::downgrade -> Weak<T>
  • RefCell: ๋‹ค์ค‘ ์†Œ์œ ๊ถŒ์„ ํ—ˆ์šฉํ•œ๋‹ค. Rc.clone()
    • .{burrow,burrow_mut} ๋ฉ”์„œ๋“œ, burrowing rule ์€ runtime์— ์ฒดํฌ๋˜๋ฉฐ ์œ„๋ฐ˜ํ•˜๋Š” ๊ฒฝ์šฐ ํŒจ๋‹‰,
    • Mutex: ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ์— ์ ‘๊ทผ์„ ์ œ์–ดํ•œ๋‹ค. thread safe
    • RefMut
    • Ref
  • Weak
    • Rc + RefCell ์กฐํ•ฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด cycle์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Œ
    • cycle ์ด ์ƒ์„ฑ๋˜๋ฉด ๋ ˆํผ๋Ÿฐ์Šค ์นด์šดํŠธ๊ฐ€ 0 ์ด ๋  ์ˆ˜ ์—†์–ด, ๋ฉ”๋ชจ๋ฆฌ ๋ฆญ์ด ๋ฐœ์ƒ
    • Weak ๋Š” Rc์ฐธ์กฐ์‹œ์— ์ƒ์„ฑ๋˜๋Š” strong_count ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค์ง€ ์•Š๊ณ  strong_count ๊ฐ€ 0์ด ๋˜๋Š” ๊ฒฝ์šฐ ํ•จ๊ฒŒ ์†Œ๋ฉธ
      • e.g. branch <-> leaf ์˜ ๊ด€๊ณ„์—์„œ ์ƒํ˜ธ ์ฐธ์กฐํ•˜์ง€๋งŒ branch๊ฐ€ ์ž˜๋ ค๋‚˜๊ฐ€๋ฉด leaf๋Š” ์กด์žฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ
      • .upgrade -> Option<Weak<T>>

array, slice, vec

  • arrray: [u8; 3] primitive ํƒ€์ž… fixed length
  • slice: &[..] array์˜ ์ฐธ์กฐ๋กœ ์กด์žฌ,
  • vec: growing array, ํ•ญ์ƒ ํž™์— ์ €์ •๋œ๋‹ค.

question

  • @todo match { Err(ref e) => ... }, ref๋ฅผ ๋ถ™์ด๋Š” ์ด์œ 
  • iter vs into_iter

์žฅ

์šฉ์–ด

  • ํ”„๋ ˆ๋ฅ˜๋“œ (Prelude)
    ๋ชจ๋“  ๋Ÿฌ์ŠคํŠธ ํ”„๋กœ๊ทธ๋žจ์— ์ •์˜๋œ ํŠน๋ณ„ํ•œ ์˜์—ญ์œผ๋กœ ํ•ด๋‹น ํ”„๋กœ๊ทธ๋žจ ๋˜๋Š” ํฌ๋ ˆ์ดํŠธ๊ฐ€ ์™ธ๋ถ€๋กœ ๋…ธ์ถœํ•˜๋Š” ํƒ€์ž…์„ ๋ช…์‹œํ•œ ๋ชจ๋“ˆ

link

โš ๏ธ **GitHub.com Fallback** โš ๏ธ