Hello World!

๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋Ÿฌ์ŠคํŠธ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ๊ณ ์ „์ ์ธ Hello World ์ถœ๋ ฅ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

Let us jump into the simplest possible Rust program, a classic Hello World program:

fn main() {
    println!("Hello ๐ŸŒ!");
}

์œ„ ์†Œ์Šค์—์„œ ์•Œ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ:

  • ํ•จ์ˆ˜๋Š” fn์œผ๋กœ ์„ ์–ธ๋ฉ๋‹ˆ๋‹ค.
  • C/C++๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ค‘๊ด„ํ˜ธ{}๋กœ ๋ธ”๋ก์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
  • main ํ•จ์ˆ˜๋Š” ํ”„๋กœ๊ทธ๋žจ ์ง„์ž…์ ์ž…๋‹ˆ๋‹ค.
  • ๋Ÿฌ์ŠคํŠธ๋Š” ์œ„์ƒ์ ์ธ ๋งคํฌ๋กœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. println!๋Š” ๊ทธ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.
  • ๋Ÿฌ์ŠคํŠธ์˜ ๋ฌธ์ž์—ด์€ UTF-8๋กœ ์ธ์ฝ”๋”ฉ๋˜๋ฉฐ ์ด๋ชจ์ง€์™€ ๊ฐ™์€ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

What you see:

  • Functions are introduced with fn.
  • Blocks are delimited by curly braces like in C and C++.
  • The main function is the entry point of the program.
  • Rust has hygienic macros, println! is an example of this.
  • Rust strings are UTF-8 encoded and can contain any Unicode character.

์—ญ์ฃผ

  • ๋งคํฌ๋กœ: ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค(meta-programming). ๋Ÿฐํƒ€์ž„์ด ์•„๋‹Œ ์ปดํŒŒ์ผ ์ „์— ๋Œ€์น˜ ์ž‘์—…์ด ์ด๋ค„์ง€๋Š”๋ฐ C๊ณ„์—ด์˜ #define ๋ฌธ๋ฒ•์„ ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • ์œ„์ƒ์ ์ธ ๋งคํฌ๋กœ(hygienic macros, ์œ„ํ‚ค)๋Š” ์‹๋ณ„์ž๊ฐ€ ๊ฒน์น˜์ง€ ์•Š์Œ์ด ๋ณด์žฅ๋˜๋Š” ๋งคํฌ๋กœโ€ฆ ๋ผ๋Š”๋ฐ ์ผ๋‹จ์€ ๊ทธ๋ ‡๊ตฌ๋‚˜ํ•˜๊ณ  ์ง„ํ–‰
๊ฐ•์˜ ์ฐธ์กฐ ๋…ธํŠธ

์ด ์Šฌ๋ผ์ด๋“œ๋Š” ํ•™์ƒ๋“ค์ด ๋Ÿฌ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ํŽธํ•˜๊ฒŒ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ์œ ๋„ํ•ฉ๋‹ˆ๋‹ค. 4์ผ๋™์•ˆ ๋งŽ์€ ์–‘์˜ ์ฝ”๋“œ๋ฅผ ๋ณผ ๊ฒƒ์ด๋ฏ€๋กœ ์นœ์ˆ™ํ•˜๊ณ  ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

ํ‚คํฌ์ธํŠธ:

  • ๋Ÿฌ์ŠคํŠธ๋Š” C/C++/Java์™€ ๊ฐ™์€ ์ „ํ†ต์ ์ธ ๋‹ค๋ฅธ ์–ธ์–ด๋“ค๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ํ•„์ˆ˜์ ์ด๊ณ  ์ ˆ๋Œ€์ ์œผ๋กœ ํ•„์š”ํ•˜์ง€ ์•Š๋Š” ํ•œ ์ƒˆ๋กœ์šด ๊ฒƒ(๋ฌธ๋ฒ•)์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๋Ÿฌ์ŠคํŠธ๋Š” ์œ ๋‹ˆ์ฝ”๋“œ์™€ ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์ „๋ถ€ ์ง€์›ํ•˜๋Š” ํ˜„๋Œ€์ ์ธ ์–ธ์–ด์ž…๋‹ˆ๋‹ค.
  • ๋Ÿฌ์ŠคํŠธ๋Š” ์ธ์ˆ˜์˜ ์ˆ˜๊ฐ€ ๋งŽ์€ ์ƒํ™ฉ์— ๋Œ€ํ•ด์„œ ๋งคํฌ๋กœ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (no function overloading).

This slide tries to make the students comfortable with Rust code. They will see a ton of it over the next four days so we start small with something familiar.

Key points:

  • Rust is very much like other languages in the C/C++/Java tradition. It is imperative (not functional) and it doesnโ€™t try to reinvent things unless absolutely necessary.

  • Rust is modern with full support for things like Unicode.

  • Rust uses macros for situations where you want to have a variable number of arguments (no function overloading).