rust - deptno/deptno.github.io GitHub Wiki
- 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์ ์ถ๊ฐํด์ ์คํ
- Cargo.toml ์ ๋จผ์
// 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 ํ์ผ์ ์ฐธ์กฐํ๋ค.
์ปดํ์ผ ๋จ์
curl https://sh.rustup.rs -sSf | shrust update
rustup self uninstall
rustup docs --bookrustc --version
rustc main.rscargo ๋ ๋ฌ์คํธ์ ํจํค์ง ๋งค๋์
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 ํ์ผ์ ํตํด ์คํํ์ผ ํน์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋น๋ ์ํธ๋ฆฌ๋ฅผ ์์ฑํ ์ ์๋ค(๋๋ค ๊ฐ๋ฅ).
- #![allow(dead_code)] - ์์ฐ๋ ์ฝ๋ ๊ฒฝ๊ณ ํ์ง ์์
- #![allow(unused)] - ์์ฐ๋ ๋ณ์ ๊ฒฝ๊ณ ํ์ง ์์
- #[allow(non_camel_case_types)] - ๊ฒฝ๊ณ ์ํจ
use std::io;- 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 ๊ฐ์ ๋ฆฌํด
-
2023-08-27 ๋ช
์์ overflow ํธ๋ค๋ง(release ๊ธฐ๋ณธ์
- float
- unit - ํํ์ด์ง๋ง primitive type ์ด๋ค.
- compound type
- array
- slice, array ์๋ ๋ค๋ฅด๊ฒ ์ปดํ์ผํ์์ length ๋ฅผ ์ ์ ์๋ค.
- tuple
- struct
- enum
- i <-> u ์ ์๊ฐ์ ๋ณํ์ ํด๋น ๊ฐ์ ํํํ๋ ํ์์ ๋ฐ๋ผ ๊ทธ๋๋ก ์ ์ฉ๋๋ค.
- floating <-> integer ๋ณํ์ floating ๋ณ์๊ฐ ํํํ๋ ๊ทผ์ฌ๊ฐ์ ๋ด๋ฆผํ์ฌ ์ ์ ๋ณํํ๋ค.
saturating cast
primitive ํ์
์ cast ๋ฅผ ํตํด์ ๋ณ๊ฒฝ๋๋ค.
custom ํ์
์ trait From ์ด๋ Into ๋ฅผ ํตํด์ ๋ณ๊ฒฝ๋๋ค.
- 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์ผ๋ก ๋ณํ๋๋ค.
- std::fmt::Display ์ ๊ตฌํํ๋ฉด
- FromStr
-
parse๋ฉ์๋ ํธ์ถ์ ํด๋น ํ์ ์ ๊ตฌํ๋์ด์๋FromStr์ ํตํด ๋ณํ๋๋ค.
let parsed: i32 = "5".parse().unwrap(); let turbo_parsed = "10".parse::<i32>().unwrap();
-
์ฐธ์กฐ๋ ์์ ๊ถ์ ๊ฐ์ง ์๋๋ค.
- ๋ถํธ ์ฐธ์กฐ๋ ์ฌ๋ฌ๊ฐ๊ฐ ๊ฐ๋ฅํ๋ค.
- ๊ฐ๋ณ ์ฐธ์กฐ๋ ํ๋๋ง ๊ฐ๋ฅํ๋ค.
- ๋ถ๋ณ ์ฐธ์กฐ๊ฐ ์กด์ฌํ๋ ๊ฒฝ์ฐ ๊ฐ๋ณ ์ฐธ์กฐ๋ ๋ถ๊ฐ๋ฅํ๋ค. -> ๊ฐ์ด ๋ณ๊ฒฝ๋๋ฉด ๋ถ๋ณ ์ฐธ์กฐ์ ๊ฐ๋ ๋ณ๊ฒฝ๋์ด ๋ฌผ๊ฒฐ์ฑ์ด ๊นจ์ง๊ธฐ ๋๋ฌธ
&String[..] === &str
- enum
- ์ฒซ์ธ์๋ก &self https://doc.rust-lang.org/rust-by-example/custom_types/enum.html
- ์ฒซ์ธ์๋ก self https://doc.rust-lang.org/rust-by-example/custom_types/enum/testcase_linked_list.html
& borrow ์ ๊ฐ๋ค.
let c = 'Q';
let ref ref_c1 = c;
let ref_c2 = &c;destructure ๋ฅผ ํตํด์ ๋ณ์๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒฝ์ฐ์๋ ์์ ๊ถ์ ์ด๋๋๋ค.
ref ํค์๋๋ฅผ prefix ๋ก ์ฌ์ฉํ๋ฉด ์์ ๊ถ ์ด๋์ ๋ง์ ์ ์๋ค. stack value ์์๋ ํจ๊ณผ๊ฐ ์๋ค.
let x = 5; # ๋ถ๋ณ
let x = 5.0; # ์ด์ ๋ณ์๋ ๊ฐ๋ ค์ง๋ค(shadowing), ์ด์ ํ์
๋ฌด์ ๊ฐ๋ฅ. ์ฌ์ฉ๋์ง ์๋ ๋ณ์๋ ์ปดํ์ผ๋ฌ๊ฐ ์ ๊ฑฐํ๋ค.
const CONSTANT: u32 = 100_000; # ํญ์ ํ์
์ง์
let mut y = String::new(); # ๊ฐ๋ณ
y = 1; # ํ์
์๋ฌ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
- sugar
์ธ์๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ฐธ์กฐ ๋น๋ฆฌ๊ธฐ(& borrow) ์์ํ์ฌ ํ์์ฑ์ด ์๊ตฌ๋ ๋๋ง ๋ ๋ฎ์ ๋จ๊ณ๋ก ๊ฐ๋ค. https://doc.rust-lang.org/rust-by-example/fn/closures/capture.html
non-copy ํ์
์ ๋ํด์ ํด๋ก์ ๋ฅผ ์ ์ธํ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก &๋ก ์ฐธ์กฐ๋๋ค.
ํ์ง๋ง mutable ํ๋(std::mem::drop ๋ฑ์ ์ฌ์ฉ)์ ์ทจํ๋ฉด move ๋๋ค.
ํน์ ๋ช
์์ ์ผ๋ก move ๋ฅผ ํ ์ ์๋ค.
move ๋ ์ดํ๋ ์์ ๊ถ์ด ์ด๋๋์๊ธฐ ๋๋ฌธ์ ์บก์ณ๋ ๋ณ์๋ฅผ ๋์ด์ ์ฌ์ฉํ ์ ์๋ค.
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 ๋ฌธ๊ณผ ํจ๊ป ๊ฐ ๋ฆฌํด์ด ๊ฐ๋ฅ
//
- 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!
String::newnew associated function ์ด๋ผ๊ณ ๋ถ๋ฆฌ๋ฉฐ ํ ์ธ์ด์ static method ์ ๊ฐ๋ค.
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!"),
}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);
}ํ ํฐ์ผ๋ก ์ฌ์ฉ๋ # ๊ฐฏ์๋ ์ ํ์ด ์์
HashMap<T, ()>
- union
- difference
- intersection
- symmetric_difference - xor
์ฌ๋ฌ ์์ ๊ถ์ด ํ์ํ ๊ฒฝ์ฐ๋ฅผ ์ํด 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 ---");
}
}
}atomic refernece counted,
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- posix::Path
- windows::path
์ํฉ์ ๋ง๊ฒ ์ฌ์ฉ๋๋ค.
to_str๋ฉ์๋๋ utf-8 ์ด ์๋๊ฒฝ์ฐ ์คํจํ ์ ์๋ค(Option).
์คํ์ค์ธ ์์ ํ๋ก์ธ์ค์ ํธ๋ค์ ๋ ธ์ถํ๋ค.
- 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,
};- env::args
#[link(name = "mylib")]
extern {
fn a(z: Complex) -> Complex;
}C ๋ฐ์ธ๋ฉ์ ์ง์, unsafe
println!("hello, world!");
println!("{}", "hello, world!");
println!("{} {}", "hello", "world");- Result
- method
- expect(value: &str)
- method
- Ordering
FromResidual ์ด ๊ตฌํ๋ ํ์
์์ ์ฌ์ฉ ๊ฐ๋ฅ, ์ ๊ณต๋๋ ํ์
์ผ๋ก๋ ์๋๊ฐ ์์
- Result<T, E>
- Option
ํจ์ ๊ฒฐ๊ด๊ฐ์ผ๋ก Error๊ฐ ๋ฆฌํด๋๋ฉด ํด๋น ํจ์๋ฅผ ํธ์ถํ๋ ํจ์์์ ๋ฆฌํด ์ฒ๋ฆฌํ๋ค.
- 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>>
- arrray: [u8; 3] primitive ํ์ fixed length
- slice: &[..] array์ ์ฐธ์กฐ๋ก ์กด์ฌ,
- vec: growing array, ํญ์ ํ์ ์ ์ ๋๋ค.
- @todo match { Err(ref e) => ... }, ref๋ฅผ ๋ถ์ด๋ ์ด์
- iter vs into_iter
- ํ๋ ๋ฅ๋ (Prelude)
๋ชจ๋ ๋ฌ์คํธ ํ๋ก๊ทธ๋จ์ ์ ์๋ ํน๋ณํ ์์ญ์ผ๋ก ํด๋น ํ๋ก๊ทธ๋จ ๋๋ ํฌ๋ ์ดํธ๊ฐ ์ธ๋ถ๋ก ๋ ธ์ถํ๋ ํ์ ์ ๋ช ์ํ ๋ชจ๋