์ฐ์ฐ
fn interproduct(a: i32, b: i32, c: i32) -> i32 { return a * b + b * c + c * a; } fn main() { println!("๊ฒฐ๊ณผ: {}", interproduct(120, 100, 248)); }
main
์ด์ธ์ ํจ์๋ ์ด๋ฒ์ด ์ฒ์์ด์ง๋ง ์๋ฏธ๋ ๋ช
ํํฉ๋๋ค. ์ธ ๊ฐ์ ์ ์๋ฅผ ์ฌ์ฉํ๊ณ ์ ์๋ฅผ ๋ฐํํฉ๋๋ค. ํจ์๋ ๋์ค์ ๋ ์์ธํ ๋ค๋ฃจ๊ฒ ์ต๋๋ค.
์ฐ์ ์ฐ์ฐ์ ๋ค๋ฅธ ์ธ์ด์ ๋งค์ฐ ์ ์ฌํ๋ฉฐ ์ฐ์ ์์๊ฐ ๋น์ทํฉ๋๋ค.
์ ์ ์ค๋ฒํ๋ก๋ ์ด๋ป๊ฒ ๋๋์? C ๋ฐ C++์์ ๋ถํธ์๋ ์ ์์ ์ค๋ฒํ๋ก๋ ์ค์ ๋ก ์ ์๋์ง ์์ผ๋ฉฐ, ๋ค๋ฅธ ํ๋ซํผ์ด๋ ์ปดํ์ผ๋ฌ์์ ๋ค๋ฅธ ์์ ์ ์คํํ ์ ์์ต๋๋ค. Rust์์๋ ์ ์ ์ค๋ฒํ๋ก ์์ ๋์์ด ์ ์๋์ด ์์ต๋๋ค.
Change the i32
โs to i16
to see an integer overflow, which panics (checked) in a debug build and wraps in a release build. There are other options, such as overflowing, saturating, and carrying. These are accessed with method syntax, e.g., (a * b).saturating_add(b * c).saturating_add(c * a)
.
์ฌ์ค ์ปดํ์ผ๋ฌ๋ ์์ ํํ์์ ์ค๋ฒํ๋ก๋ฅผ ๊ฐ์งํ๋ฏ๋ก ์ด ์์์๋ ๋ณ๋์ ํจ์๊ฐ ํ์ํฉ๋๋ค.