ํ…Œ์ŠคํŠธ

Rust์—๋Š” ์•”์‹œ์  ํƒ€์ž… ๋ณ€ํ™˜์ด ์—†์ง€๋งŒ as๋ฅผ ์‚ฌ์šฉํ•œ ๋ช…์‹œ์  ๋ณ€ํ™˜์€ ์ง€์›๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ C ์˜๋ฏธ๋ก ์„ ๋”ฐ๋ผ ์ •์˜๋ฉ๋‹ˆ๋‹ค.

fn main() {
    let value: i64 = 1000;
    println!("as u16: {}", value as u16);
    println!("as i16: {}", value as i16);
    println!("as u8: {}", value as u8);
}

as์˜ ๊ฒฐ๊ณผ๋Š” Rust์—์„œ ํ•ญ์ƒ ์ •์˜๋˜๋ฉฐ ์—ฌ๋Ÿฌ ํ”Œ๋žซํผ์—์„œ ์ผ๊ด€๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๊ธฐํ˜ธ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ๋” ์ž‘์€ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ์˜ ์ง๊ด€๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜๊ณ  ๋ช…ํ™•ํ•˜๊ฒŒ ์„ค๋ช…ํ•ด ์ฃผ์„ธ์š”.

Casting with as is a relatively sharp tool that is easy to use incorrectly, and can be a source of subtle bugs as future maintenance work changes the types that are used or the ranges of values in types. Casts are best used only when the intent is to indicate unconditional truncation (e.g. selecting the bottom 32 bits of a u64 with as u32, regardless of what was in the high bits).

For infallible casts (e.g. u32 to u64), prefer using From or Into over as to confirm that the cast is in fact infallible. For fallible casts, TryFrom and TryInto are available when you want to handle casts that fit differently from those that donโ€™t.

This slide should take about 5 minutes.

์ด ์Šฌ๋ผ์ด๋“œ๊ฐ€ ๋๋‚œ ํ›„ ์ž ์‹œ ์‰ฌ์–ด๊ฐ€๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

as is similar to a C++ static cast. Use of as in cases where data might be lost is generally discouraged, or at least deserves an explanatory comment.

์ด๋Š” ์ •์ˆ˜๋ฅผ usize๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ƒ‰์ธ์œผ๋กœ ์‚ฌ์šฉํ•  ๋•Œ ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค.