ํ•จ์ˆ˜

fn gcd(a: u32, b: u32) -> u32 {
    if b > 0 {
        gcd(b, a % b)
    } else {
        a
    }
}

fn main() {
    println!("gcd: {}", gcd(143, 52));
}
This slide should take about 3 minutes.
  • ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ์—๋Š” ์ด๋ฆ„์„ ๋จผ์ € ์“ฐ๊ณ , ํƒ€์ž…์„ ๋‚˜์ค‘์— ์”๋‹ˆ๋‹ค. ์ด๋ฆ„๊ณผ ํƒ€์ž…์€ : ๋กœ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ผ๋ถ€ ์–ธ์–ด(์˜ˆ๋ฅผ ๋“ค์–ด C)์™€ ๋ฐ˜๋Œ€์ž„์— ์œ ์˜ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๋ฆฌํ„ด ํƒ€์ž…๋„ ํ•จ์ˆ˜์˜ ์‹œ์ž‘์ด ์•„๋‹Œ ๊ฐ€์žฅ ๋’ท๋ถ€๋ถ„์— ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.
  • The last expression in a function body (or any block) becomes the return value. Simply omit the ; at the end of the expression. The return keyword can be used for early return, but the โ€œbare valueโ€ form is idiomatic at the end of a function (refactor gcd to use a return).
  • ๋ฐ˜ํ™˜๊ฐ’์ด ์—†๋Š” ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ, ์œ ๋‹› ํƒ€์ž… ()์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. -> ()๊ฐ€ ์ƒ๋žต๋œ ๊ฒฝ์šฐ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ด๋ฅผ ์ถ”๋ก ํ•ฉ๋‹ˆ๋‹ค.
  • Overloading is not supported โ€“ each function has a single implementation.
    • ํ•ญ์ƒ ๊ณ ์ •๋œ ๊ฐœ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์ธ์ˆ˜๋Š” ์ง€์›๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋งคํฌ๋กœ๋Š” ๊ฐ€๋ณ€ ํ•จ์ˆ˜๋ฅผ ์ง€์›ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • Always takes a single set of parameter types. These types can be generic, which will be covered later.