๐ฆRust syntax
๋ณต์กํ์ง๋ง ์๋ฆ๋ต๋ค
์ด๋ ์ธ์ด๋ฅผ ๋ฐฐ์ฐ๊ฑด ๊ธฐ๋ณธ๋ฌธ๋ฒ์ ๋น์ทํ๋ค. Rust๋ ์์ธ๋ ์๋๋ค.
๊ทธ๋์ ์ญ์๋ ์ฃผ์ํ ๋งํ ์ฌํญ ์์ฃผ๋ก ๊ฐ๋จํ๊ฒ ์ ๋ฆฌํ๋ คํ๋ค.
Scala ํ์ ๊ณผ Compound ํ์
Rust๋ ํ์
์งํฅ์ ์ธ์ด
์ด๋ฉฐ scala ํ์
๊ณผ compount ํ์
์ด ์๋ค.
scala
๋ ๋ค๋ฅธ ์ธ์ด์ primitive ํ์
๊ณผ ์๋ฏธ๊ฐ ๋น์ทํ๋ฉฐ rust๊ฐ ์๋์ผ๋ก stack memory์ ํ ๋น์ ํ๋ค.
compound
๋ tuple์ด๋ array๊ฐ์ ๋ณตํฉ์ฒด๋ก heap memory์ ํ ๋น๋๋ค.
๋ณ์์ ์ ์ธ : let๊ณผ mut
let a = 5;
a = 6; // compile error
let mut b = 5;
b = 6; // Okay
let
์ ๋ณ๊ฒฝํ ์ ์๋ ๋ณ์(?)์ ์ ์ธ์ด๋ค. (const๊ฐ ์์๋ฅผ ์ํด์ ๋ฐ๋ก ์๋ค๊ณ ํด์ ์ด์ํ ์ค๋ช
์ด ๋์ด๋ฒ๋ ธ๋ค)
๊ธฐ๋ณธ์ ์ผ๋ก ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ฒ๋ผ ๋ถ๋ณ์ฑ์ ๊ธฐ๋ณธ์ผ๋ก ํ๋ ค๋ ๊ฒ ๊ฐ๋ค.
let
์ mut
๋ฅผ ๋ถ์ด๋ฉด ๋น๋ก์ ๋ณ์๊ฐ ๋๋๋ฐ mutable์ด๋ ๋ป์ด๋ค.
์ ์ํ์ ๊ธฐ๋ณธํ์ 32bit๋ก ํ์
์ ์ธ๊ธํ์ง ์๋๋ค๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ์ ์๋๋ค.
let mut a = 2147483647; // 32bit
a = a + 1; // compile error
a๋ ํ์
์ ์ ์ํ์ง์์ ๊ธฐ๋ณธ์ธ 32bit์ด๋ค. compileํด๋ณด๋ฉด compiler๋ ๋ณ์์ overflow๋ round robin์ ํ์ฉํ์ง ์๋๋ค. warning์ด ์๋๋ผ compile error
๊ฐ ๋ฐ์ํ๋ค. ๋งค์ฐ ์๊ฒฉํ๋ค.
let mut a:i64 = 2147483647;
a = a + 1; // Okay
๋ณ์ ๋ค์ ์์ ๊ฐ์ด ํ์ ์ง์ ์ ํ ์ ์๊ณ a๋ 64๋นํธ์ด๋ฏ๋ก ์ ์์ ์ผ๋ก ๋์ํ๋ค.
let x = 2.0; // 64bit
let y:f32 = 3.0;
๋ฐ๋๋ก ์ค์ํ์ ๊ฒฝ์ฐ 64bit๊ฐ ๊ธฐ๋ณธ์ด๋ค.
C์ธ์ด์์ double์ด ๊ธฐ๋ณธ์ด๊ณ ๋น ๋ฅธ ๊ฒ๊ณผ ๋น์ทํด๋ณด์ธ๋ค. ์ปดํจํฐ๊ณตํ์ ์ผ๋ก ๋น์ฐํ ์ด์ผ๊ธฐ์ด๋ค.
let months = ["Jan", "Feb"];
println!("{}", months[3]); // compile error
๋จผ์ ์ค๋ช
์ด ๋น ์ง ๋ถ๋ถ์ด ์์๋๋ฐ ์ { }๋ ์์ํ๋ฏ placeholder์ด๋ค.
(like %d in C language)
๊ทธ๋ฆฌ๊ณ ์ค์ํ ๊ฒ์ ์ด ์ฝ๋๋ [3]์์ overflow๊ฐ ๋ฐ์ํ๋๋ฐ compile error
๊ฐ ๋ฐ์ํ๋ค.
์ฆ, ์๋ชป๋ memory access๋ฅผ ๋ฏธ๋ฆฌ ๋ง๋๋ค.
ํ๋ฉด ํ ์๋ก ๋ฌธ๋ฒ๋ณด๋ค๋ rust๊ฐ ์ผ๋ง๋ ์๊ฒฉํ์ง์ ๋ํ ์ค๋ช
์ด ๋์ด๊ฐ๋ค.
ํ์ง๋ง ์ง์ ๋ถํ์ง์๊ณ ๋ชจ๋ํ๋ค.
ํจ์
fn main() {
another_function(3);
}
fn another_function(x:i32) -> i32 {
println!("x={}", x);
x + 1
}
ํจ์๊ฐ ๋ฏธ๋ฆฌ ์์ ์ ์ธ๋ ํ์๊ฐ ์๋ค(๋ชจ๋ํ์ง?)
๊ทธ๋ฆฌ๊ณ return value๋ ๋ง์ง๋ง์ expression
์ด๋ค. ์ธ๋ฏธ์ฝ๋ก ์ ๊น๋จน์ ๊ฒ์ด ์๋๋ค.
(์คํ๋ ค ;๋ฅผ ๋ถ์ด๋ฉด expression์ด ์๋ statement๊ฐ ๋์ด์ ์๋ฌ๊ฐ ๋๋ค)
๊ทธ๋ฆฌ๊ณ ๋ฌผ๋ก ๋ค๋ฅธ ์ธ์ด์ฒ๋ผ ๋ช ์์ ์ผ๋ก return์ ์ธ์๋ ์๋ค.
Tuple
tuple
์ ์๋์ ๊ฐ์ด ์ฌ์ฉ๋๋ฉฐ ๋ณตํฉ์ ์ธ ํ์
์ด ์ฌ์ฉ๋ ์ ์๋ค.
(Modern C++ ๋ฑ์์๋ ์ ๊ณต๋๋ ๊ทธ tuple์ด๋ค)
fn main() {
let x: (i32, f64, u8) = (500, 6.4, 1);
let five_hundred = x.0;
let six_point_four = x.1;
let one = x.2;
}
๊ทธ๋ฆฌ๊ณ ๋ฐฐ์ด,
fn main() {
let a = [1,2,3,4,5];
for element in a.iter() {
if element == 3 {
break;
}
}
}
๋ฐฐ์ด์๋ method์ธ iter()
๊ฐ ์์๊ณผ if, while ๋ฑ์ ์กฐ๊ฑด์๋ ๊ดํธ๊ฐ ์์์ ์ฃผ๋ชฉํ์.
(์์ผ๋ฉด ์คํ๋ ค warning์ด ๋๋ค)
์๋๋ฉด ์ฌ๊ธฐ์ if๋ expression์ด๋ค. ๋ฐ๋ผ์ let๊ณผ ํจ๊ป ์๋์ฒ๋ผ ์ฌ์ฉํ ์ ์๋ค.
let number = if condition {
5
} else {
6
};
์ผํญ์ฐ์ฐ์๋ณด๋ค ๋ ๊น๋ํด๋ณด์ธ๋ค.
expression์ ๊ฐ๋
์ ์์๋ณด๊ธฐ ์ํด ์์ ๋ฅผ ํ๋ ๋ ์ดํด๋ณด์.
fn main() {
let x = 5;
// let x = (let y = 6); // compile error
let y = {
let x = 3;
x + 1
};
println!("The value of y is: {}", y);
}
๊ตฌ์กฐ์ฒดโฆ์ฌ์ค์ ํด๋์ค
์ด๋ฒ์๋ ๊ตฌ์กฐ์ฒด๋ฅผ ์์๋ณธ๋ค.
(method๋ ์์ผ๋ ์ฌ์ค์ ํด๋์ค๋ค)
fn main() {
struct User {
username: String,
email: string,
sign_in_count: u64,
active: bool
}
impl User {
fn get_active(&self) -> bool { // first parameter must be &self
self.active
}
}
let user1 = User {
email: String::from("example@xyz.com");
username: String::from("hoo");
active: true,
sign_in_count: 1
};
let user2 = User {
active: false,
..user1 // email, username => move from user1
}
println!("{}", user1.email); // compile error => user1.email move
}
method์ ์ฒซ๋ฒ์งธ ์ธ์๋ก &self
๋ฅผ ๋ฐ๋ ์
(C++์ this์ ๊ฐ๋ค. ์ฌ๋ฌ๋ชจ๋ก C++ ์ ์ ๊ฐ ํ์
ํ๊ธฐ ์ฌ์ด ์ธ์ด๊ฐ๋ค)
method๊ฐ impl๋ก ๋ฐ๋ก ๋จ์ด์ ธ์๋ ์
(C++์์ ์ ์ธ๊ณผ ๊ตฌํ์ ๋๋๋ ๊ฒ๊ณผ ์ ์ฌํด๋ณด์ธ๋ค. data structure๋ง ๊น๋ํ๊ฒ ๋ฐ๋ก ๋ณผ ์ ์๋ค)