μ°μ°
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)
.
μ¬μ€ μ»΄νμΌλ¬λ μμ ννμμ μ€λ²νλ‘λ₯Ό κ°μ§νλ―λ‘ μ΄ μμμλ λ³λμ ν¨μκ° νμν©λλ€.