Updated getting-started.md

AinL authored
revision bb224352c112094acb189130d2ed659d221095ad
getting-started
# 2. 시작하기(Getting Started) - 520%

이 책의 첫 번째 부분을 거치며 당신이 Rust, 그리고 연관된 연장들을 챙기게 될 거에요. 먼저 우리는 Rust를 설치할 것입니다. 그 다음은 전통적인 "안녕, 세상아_Hello, World_" 프로그램이죠. 마지막으로, Rust의 빌드 시스템이자 패키지 매니저인 Cargo에 대해서 이야기할 것입니다.

# Rust 설치하기

첫 번째로 Rust를 사용하기 위해선 설치를 하여야합니다. 우선 우리가 Rust 를 다운로드 하기위해선 인터넷에 연결되어 있어야합니다.

우리는 터미널에서 사용되는 몇개의 명령어의 시작을 `$` 를 표시하여 강조 할 것입니다.
하지만 `$` 는 해당 라인이 커맨드라는 사실을 나타낼 뿐 이므로 실제로 입력하지 않아도 됩니다.
우리는 인터넷에서 많은 튜토리얼이나 예제에서 `$` 는 보통 유저(일반적인 권한을 가진 유저) 가 실행하는 커맨드들의 앞에 표시하고, `#`은 관리자(관리자 권한을 가진 유저)를 통해 실행되어야 하는 명령어들 앞에 표시하는 관습을 따릅니다.


## 플렛폼 지원

'러스트' 는 수 많은 플렛폼에서 컴파일되고, 실행 될 수 있습니다. 러스트가 지원하는 플랫폼들은 크게 세 가지로 나뉠 수 있습니다.


### 단계 1

1단계 플렛폼들은 러스트 코드 빌드와 프로그램을 만들 수 있는 플렛폼입니다.

요구사항 -
- 플렛폼에서의 작동을 검증함.
- `rustlang/rust` 레퍼토리의 master branch의 태스트를 통과함.
- 플렛폼을 공식적으로 지원함.
- 그 플렛폼에서 러스트를 빌드하고, 사용하는 문서가 작성되있음.


| 타겟 플렛폼 | std |rustc|cargo| 비고 |
|-------------------------------|-----|-----|-----|----------------------------|
| `x86_64-pc-windows-msvc` | ✓ | ✓ | ✓ | 64-bit MSVC (Windows 7+) |
| `i686-pc-windows-gnu` | ✓ | ✓ | ✓ | 32-bit MinGW (Windows 7+) |
| `x86_64-pc-windows-gnu` | ✓ | ✓ | ✓ | 64-bit MinGW (Windows 7+) |
| `i686-apple-darwin` | ✓ | ✓ | ✓ | 32-bit OSX (10.7+, Lion+) |
| `x86_64-apple-darwin` | ✓ | ✓ | ✓ | 64-bit OSX (10.7+, Lion+) |
| `i686-unknown-linux-gnu` | ✓ | ✓ | ✓ | 32-bit Linux (2.6.18+) |
| `x86_64-unknown-linux-gnu` | ✓ | ✓ | ✓ | 64-bit Linux (2.6.18+) |

### Tier 2

2단계 플렛폼들은 러스트 코드를 빌드 할수있는 플렛폼입니다. 이 단계에 해당된 플렛폼을 위한 검증작업은 행해지지 않고, 그럼으로 이 플렛폼들에서의 완벽한 작동은 보장하지 못합니다.
하지만 이 플렛폼에서의 작동을 위한 당신의 노력은 언제나 환영합니다!

요구사항-
- 플렛폼에서의 작동을 검증할지도 모름.
- `rustlang/rust` 레퍼토리의 master branch의 태스트를 통과했지만, 하지만 표준 라이브러리로만 검증함.
- 플렛폼을 공식적으로 지원함.


| 타겟 플렛폼 | std |rustc|cargo| 비고 |
|-------------------------------|-----|-----|-----|----------------------------|
| `i686-pc-windows-msvc` | ✓ | ✓ | ✓ | 32-bit MSVC (Windows 7+) |

### 3단계

3단계 플랫폼들은 러스트를 지원하나, 최신 버전이 아니고 플렛폼에서의 빌드와 검증 작업도 이루어 지지 않은 플렛폼들을 말합니다. 이러한 플렛폼들을 위한 러스트 빌드는 커뮤니티에 의해 지원 될수도 있습니다. 또한 포함 패키지들과 인스톨러는 공식적으로 제공되지 않으며, 이 또한 커뮤니티에 의해 지원 될수도 있습니다.

| 타겟 플렛폼 | std |rustc|cargo| 비고 |
|-------------------------------|-----|-----|-----|----------------------------|
| `x86_64-unknown-linux-musl` | ✓ | | | 64-bit Linux with MUSL |
| `arm-linux-androideabi` | ✓ | | | ARM Android |
| `i686-linux-android` | ✓ | | | 32-bit x86 Android |
| `aarch64-linux-android` | ✓ | | | ARM64 Android |
| `arm-unknown-linux-gnueabi` | ✓ | ✓ | | ARM Linux (2.6.18+) |
| `arm-unknown-linux-gnueabihf` | ✓ | ✓ | | ARM Linux (2.6.18+) |
| `aarch64-unknown-linux-gnu` | ✓ | | | ARM64 Linux (2.6.18+) |
| `mips-unknown-linux-gnu` | ✓ | | | MIPS Linux (2.6.18+) |
| `mipsel-unknown-linux-gnu` | ✓ | | | MIPS (LE) Linux (2.6.18+) |
| `powerpc-unknown-linux-gnu` | ✓ | | | PowerPC Linux (2.6.18+) |
| `i386-apple-ios` | ✓ | | | 32-bit x86 iOS |
| `x86_64-apple-ios` | ✓ | | | 64-bit x86 iOS |
| `armv7-apple-ios` | ✓ | | | ARM iOS |
| `armv7s-apple-ios` | ✓ | | | ARM iOS |
| `aarch64-apple-ios` | ✓ | | | ARM64 iOS |
| `i686-unknown-freebsd` | ✓ | ✓ | | 32-bit FreeBSD |
| `x86_64-unknown-freebsd` | ✓ | ✓ | | 64-bit FreeBSD |
| `x86_64-unknown-openbsd` | ✓ | ✓ | | 64-bit OpenBSD |
| `x86_64-unknown-netbsd` | ✓ | ✓ | | 64-bit NetBSD |
| `x86_64-unknown-bitrig` | ✓ | ✓ | | 64-bit Bitrig |
| `x86_64-unknown-dragonfly` | ✓ | ✓ | | 64-bit DragonFlyBSD |
| `x86_64-rumprun-netbsd` | ✓ | | | 64-bit NetBSD Rump Kernel |
| `i686-pc-windows-msvc` (XP) | ✓ | | | Windows XP support |
| `x86_64-pc-windows-msvc` (XP) | ✓ | | | Windows XP support |

이 표는 시간이 지남에 따라 계속 늘어날것이고, 이것들만이 3단계 플렛폼이란건 아닙니다!


## 리눅스와 맥에서의 러스트 설치

당신이 리눅스 또는 맥을 쓰고 있다면, 설치를 위해 할것은 다음의 명령어를 터미널에 입력하는 것 뿐입니다.

```bash
$ curl -sSf https://static.rust-lang.org/rustup.sh | sh
```

이것은 설치 스크립트를 다운로드하여 설치를 진행하게 해줄 것입니다. 그리고 이 작업이 모두 잘 진행된다면 다음의 메세지가 나타 날 것입니다.

```text
Welcome to Rust.

This script will download the Rust compiler and its package manager, Cargo, and
install them to /usr/local. You may install elsewhere by running this script
with the --prefix= option.

The installer will run under ‘sudo’ and may ask you for your password. If you do
not want the script to run ‘sudo’ then pass it the --disable-sudo flag.

You may uninstall later by running /usr/local/lib/rustlib/uninstall.sh,
or by running this script again with the --uninstall flag.

Continue? (y/N)
```

여기서 `y` (예)를 입력하고 편안히 설치를 기달리시면 됩니다.

## 윈도우에서의 러스트 설치

당신이 윈도우를 사용하고 있다면, 윈도우용 인스톨러를 받아서 사용해주세요 [installer][install-page].

[install-page]: https://www.rust-lang.org/install.html

## 러스트 제거

러스트를 제거하는 방법은 굉장히 쉽습니다. 리눅스와 맥에서 단지 언인스톨러 스크립트를 실행하기만 하면됩니다.

```bash
$ sudo /usr/local/lib/rustlib/uninstall.sh
```

만약 당신이 윈도우의 편리한 인스톨러의 노예라면, 다시 `.msi`확장자를 가진 러스트 인스톨러를 다시 실행하여 제거 버튼을 누르면 됩니다.

## 문제 해결

러스트를 인스톨한 후 터미널을 열어 다음 명령어를 실행해 보십시오.

```bash
$ rustc --version
```

당신은 러스트의 버전과, 커밋 해쉬, 커밋 날짜를 볼수 있어야됩니다.

그것이 성공하면 러스트는 성공적으로 인스톨 된 것입니다!

만약 당신이 윈도우를 사용하고, 위 작업이 실패 했으면, 시스탬 %PATH% 변수안에 러스트 설치 경로가 들어있는지 확인하세요. 설치경로가 포함되있지 않으면, 러스트를 설치한 폴더의 경로를 추가 해주시길 바랍니다. _(어떻게 하는진 다들 아실거라 믿습니다)_

불행하게도 윈도우 이외의 플렛폼에서 설치가 실패했다면, 도움을 받을 수 있는 곳이 있습니다. 가장 쉬운곳은 공식 [irc.mozilla.org의 #rust IRC 체널][irc] 입니다. IRC 클라이언트는 [Mibbit][mibbit] 을 사용하실수 있습니다. 다른 곳으론, [사용자 포럼][users]과 [(우리들의 친구) Stack Overflow][stackoverflow]가 있습니다.

[irc]: irc://irc.mozilla.org/#rust
[mibbit]: http://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust
[users]: https://users.rust-lang.org/
[stackoverflow]: http://stackoverflow.com/questions/tagged/rust

러스트 인스톨러는 자동적으로 공식 문서를 로컬에 저장 해줍니다. 유닉스기반 운영체제라면 `/usr/local/share/doc/rust`에, 윈도우 기반이라면 `(러스트 인스톨 경로)/share/doc`에서 찾을 수 있습니다.

# Hello, world!

이제 러스트를 설치했으니, 간단한 첫 프로그램을 만들어 봅시다. 암묵적인 세계적 프로그래머 전통을 따라서 우리는 작고 아름다운 _(우리모두가 아는.)_ 'Hello, world!' 프로그램을 만들어 볼 것입니다.

이러한 작은 프로그램을 만들어보는건 컴파일러가 잘 작동하는지 확인할 수 있습니다. 그리고 화면에 문자를 출력하는건 가장 일반적인 것들임으로 빨리 해볼수록 좋죠.

> Note: This book assumes basic familiarity with the command line. Rust itself
> makes no specific demands about your editing, tooling, or where your code
> lives, so if you prefer an IDE to the command line, that's an option. You may
> want to check out [SolidOak], which was built specifically with Rust in mind.
> There are a number of extensions in development by the community, and the
> Rust team ships plugins for [various editors]. Configuring your editor or
> IDE is out of the scope of this tutorial, so check the documentation for your
> specific setup.

[SolidOak]: https://github.com/oakes/SolidOak
[various editors]: https://github.com/rust-lang/rust/blob/master/src/etc/CONFIGS.md

## 프로젝트 파일 생성

First, make a file to put your Rust code in. Rust doesn't care where your code
lives, but for this book, I suggest making a *projects* directory in your home
directory, and keeping all your projects there. Open a terminal and enter the
following commands to make a directory for this particular project:

```bash
$ mkdir ~/projects
$ cd ~/projects
$ mkdir hello_world
$ cd hello_world
```

> Note: If you’re on Windows and not using PowerShell, the `~` may not work.
> Consult the documentation for your shell for more details.

## 러스트 프로그램을 작성하고 실행하기

Next, make a new source file and call it *main.rs*. Rust files always end
in a *.rs* extension. If you’re using more than one word in your filename, use
an underscore to separate them; for example, you'd use *hello_world.rs* rather
than *helloworld.rs*.

Now open the *main.rs* file you just created, and type the following code:

```rust
fn main() {
println!("Hello, world!");
}
```

Save the file, and go back to your terminal window. On Linux or OSX, enter the
following commands:

```bash
$ rustc main.rs
$ ./main
Hello, world!
```

In Windows, just replace `main` with `main.exe`. Regardless of your operating
system, you should see the string `Hello, world!` print to the terminal. If you
did, then congratulations! You've officially written a Rust program. That makes
you a Rust programmer! Welcome.

## 간단한 러스트 문법

Now, let’s go over what just happened in your "Hello, world!" program in
detail. Here's the first piece of the puzzle:

```rust
fn main() {

}
```

These lines define a *function* in Rust. The `main` function is special: it's
the beginning of every Rust program. The first line says, “I’m declaring a
function named `main` that takes no arguments and returns nothing.” If there
were arguments, they would go inside the parentheses (`(` and `)`), and because
we aren’t returning anything from this function, we can omit the return type
entirely.

Also note that the function body is wrapped in curly braces (`{` and `}`). Rust
requires these around all function bodies. It's considered good style to put
the opening curly brace on the same line as the function declaration, with one
space in between.

Inside the `main()` function:

```rust
println!("Hello, world!");
```

This line does all of the work in this little program: it prints text to the
screen. There are a number of details that are important here. The first is
that it’s indented with four spaces, not tabs.

The second important part is the `println!()` line. This is calling a Rust
*[macro]*, which is how metaprogramming is done in Rust. If it were calling a
function instead, it would look like this: `println()` (without the !). We'll
discuss Rust macros in more detail later, but for now you just need to
know that when you see a `!` that means that you’re calling a macro instead of
a normal function.


[macro]: macros.html

Next is `"Hello, world!"` which is a *string*. Strings are a surprisingly
complicated topic in a systems programming language, and this is a *[statically
allocated]* string. We pass this string as an argument to `println!`, which
prints the string to the screen. Easy enough!

[statically allocated]: the-stack-and-the-heap.html

The line ends with a semicolon (`;`). Rust is an *[expression oriented]*
language, which means that most things are expressions, rather than statements.
The `;` indicates that this expression is over, and the next one is ready to
begin. Most lines of Rust code end with a `;`.

[expression-oriented language]: glossary.html#expression-oriented-language

## 컴파일과 실행은 다른 단계로 이루어짐

In "Writing and Running a Rust Program", we showed you how to run a newly
created program. We'll break that process down and examine each step now.

Before running a Rust program, you have to compile it. You can use the Rust
compiler by entering the `rustc` command and passing it the name of your source
file, like this:

```bash
$ rustc main.rs
```

If you come from a C or C++ background, you'll notice that this is similar to
`gcc` or `clang`. After compiling successfully, Rust should output a binary
executable, which you can see on Linux or OSX by entering the `ls` command in
your shell as follows:

```bash
$ ls
main main.rs
```

On Windows, you'd enter:

```bash
$ dir
main.exe main.rs
```

This shows we have two files: the source code, with an `.rs` extension, and the
executable (`main.exe` on Windows, `main` everywhere else). All that's left to
do from here is run the `main` or `main.exe` file, like this:

```bash
$ ./main # or main.exe on Windows
```

If *main.rs* were your "Hello, world!" program, this would print `Hello,
world!` to your terminal.

If you come from a dynamic language like Ruby, Python, or JavaScript, you may
not be used to compiling and running a program being separate steps. Rust is an
*ahead-of-time compiled* language, which means that you can compile a program,
give it to someone else, and they can run it even without Rust installed. If
you give someone a `.rb` or `.py` or `.js` file, on the other hand, they need
to have a Ruby, Python, or JavaScript implementation installed (respectively),
but you only need one command to both compile and run your program. Everything
is a tradeoff in language design.

Just compiling with `rustc` is fine for simple programs, but as your project
grows, you'll want to be able to manage all of the options your project has,
and make it easy to share your code with other people and projects. Next, I'll
introduce you to a tool called Cargo, which will help you write real-world Rust
programs.

# Hello, Cargo!

Cargo is Rust’s build system and package manager, and Rustaceans use Cargo to
manage their Rust projects. Cargo manages three things: building your code,
downloading the libraries your code depends on, and building those libraries.
We call libraries your code needs ‘dependencies’ since your code depends on
them.

The simplest Rust programs don’t have any dependencies, so right now, you'd
only use the first part of its functionality. As you write more complex Rust
programs, you’ll want to add dependencies, and if you start off using Cargo,
that will be a lot easier to do.

As the vast, vast majority of Rust projects use Cargo, we will assume that
you’re using it for the rest of the book. Cargo comes installed with Rust
itself, if you used the official installers. If you installed Rust through some
other means, you can check if you have Cargo installed by typing:

```bash
$ cargo --version
```

Into a terminal. If you see a version number, great! If you see an error like
‘`command not found`’, then you should look at the documentation for the system
in which you installed Rust, to determine if Cargo is separate.

## Cargo로 변환하기

Let’s convert the Hello World program to Cargo. To Cargo-fy a project, you need
to do three things:

1. Put your source file in the right directory.
2. Get rid of the old executable (`main.exe` on Windows, `main` everywhere else)
and make a new one.
3. Make a Cargo configuration file.

Let's get started!

### 새로운 실행 파일과 소스 폴더를 생성하기

First, go back to your terminal, move to your *hello_world* directory, and
enter the following commands:

```bash
$ mkdir src
$ mv main.rs src/main.rs
$ rm main # or 'del main.exe' on Windows
```

Cargo expects your source files to live inside a *src* directory, so do that
first. This leaves the top-level project directory (in this case,
*hello_world*) for READMEs, license information, and anything else not related
to your code. In this way, using Cargo helps you keep your projects nice and
tidy. There's a place for everything, and everything is in its place.

Now, copy *main.rs* to the *src* directory, and delete the compiled file you
created with `rustc`. As usual, replace `main` with `main.exe` if you're on
Windows.

This example retains `main.rs` as the source filename because it's creating an
executable. If you wanted to make a library instead, you'd name the file
`lib.rs`. This convention is used by Cargo to successfully compile your
projects, but it can be overridden if you wish.

### 설정 파일 생성하기

Next, create a new file inside your *hello_world* directory, and call it
`Cargo.toml`.

Make sure to capitalize the `C` in `Cargo.toml`, or Cargo won't know what to do
with the configuration file.

This file is in the *[TOML]* (Tom's Obvious, Minimal Language) format. TOML is
similar to INI, but has some extra goodies, and is used as Cargo’s
configuration format.

[TOML]: https://github.com/toml-lang/toml

Inside this file, type the following information:

```toml
[package]

name = "hello_world"
version = "0.0.1"
authors = [ "Your name " ]
```

The first line, `[package]`, indicates that the following statements are
configuring a package. As we add more information to this file, we’ll add other
sections, but for now, we just have the package configuration.

The other three lines set the three bits of configuration that Cargo needs to
know to compile your program: its name, what version it is, and who wrote it.

Once you've added this information to the *Cargo.toml* file, save it to finish
creating the configuration file.

## Cargo프로젝트를 빌드하고 실행하기

With your *Cargo.toml* file in place in your project's root directory, you
should be ready to build and run your Hello World program! To do so, enter the
following commands:

```bash
$ cargo build
Compiling hello_world v0.0.1 (file:///home/yourname/projects/hello_world)
$ ./target/debug/hello_world
Hello, world!
```

Bam! If all goes well, `Hello, world!` should print to the terminal once more.

You just built a project with `cargo build` and ran it with
`./target/debug/hello_world`, but you can actually do both in one step with
`cargo run` as follows:

```bash
$ cargo run
Running `target/debug/hello_world`
Hello, world!
```

Notice that this example didn’t re-build the project. Cargo figured out that
the file hasn’t changed, and so it just ran the binary. If you'd modified your
source code, Cargo would have rebuilt the project before running it, and you
would have seen something like this:

```bash
$ cargo run
Compiling hello_world v0.0.1 (file:///home/yourname/projects/hello_world)
Running `target/debug/hello_world`
Hello, world!
```

Cargo checks to see if any of your project’s files have been modified, and only
rebuilds your project if they’ve changed since the last time you built it.

With simple projects, Cargo doesn't bring a whole lot over just using `rustc`,
but it will become useful in future. With complex projects composed of multiple
crates, it’s much easier to let Cargo coordinate the build. With Cargo, you can
just run `cargo build`, and it should work the right way.

## 프로그램 릴리즈

When your project is finally ready for release, you can use `cargo build
--release` to compile your project with optimizations. These optimizations make
your Rust code run faster, but turning them on makes your program take longer
to compile. This is why there are two different profiles, one for development,
and one for building the final program you’ll give to a user.

Running this command also causes Cargo to create a new file called
*Cargo.lock*, which looks like this:

```toml
[root]
name = "hello_world"
version = "0.0.1"
```

Cargo uses the *Cargo.lock* file to keep track of dependencies in your
application. This is the Hello World project's *Cargo.lock* file. This project
doesn't have dependencies, so the file is a bit sparse. Realistically, you
won't ever need to touch this file yourself; just let Cargo handle it.

That’s it! If you've been following along, you should have successfully built
`hello_world` with Cargo.

Even though the project is simple, it now uses much of the real tooling you’ll
use for the rest of your Rust career. In fact, you can expect to start
virtually all Rust projects with some variation on the following commands:

```bash
$ git clone someurl.com/foo
$ cd foo
$ cargo build
```

## 무작정 따라하는 Cargo프로젝트 만들기

You don’t have to go through that previous process every time you want to start
a new project! Cargo can quickly make a bare-bones project directory that you
can start developing in right away.

To start a new project with Cargo, enter `cargo new` at the command line:

```bash
$ cargo new hello_world --bin
```

This command passes `--bin` because the goal is to get straight to making an
executable application, as opposed to a library. Executables are often called
*binaries* (as in `/usr/bin`, if you’re on a Unix system).

Cargo has generated two files and one directory for us: a `Cargo.toml` and a
*src* directory with a *main.rs* file inside. These should look familliar,
they’re exactly what we created by hand, above.

This output is all you need to get started. First, open `Cargo.toml`. It should
look something like this:

```toml
[package]

name = "hello_world"
version = "0.1.0"
authors = ["Your Name "]
```

Cargo has populated *Cargo.toml* with reasonable defaults based on the arguments
you gave it and your `git` global configuration. You may notice that Cargo has
also initialized the `hello_world` directory as a `git` repository.

Here’s what should be in `src/main.rs`:

```rust
fn main() {
println!("Hello, world!");
}
```

Cargo has generated a "Hello World!" for you, and you’re ready to start coding!

> Note: If you want to look at Cargo in more detail, check out the official [Cargo
guide], which covers all of its features.

[Cargo guide]: http://doc.crates.io/guide.html

# 마치며

This chapter covered the basics that will serve you well through the rest of
this book, and the rest of your time with Rust. Now that you’ve got the tools
down, we'll cover more about the Rust language itself.

You have two options: Dive into a project with ‘[Learn Rust][learnrust]’, or
start from the bottom and work your way up with ‘[Syntax and
Semantics][syntax]’. More experienced systems programmers will probably prefer
‘Learn Rust’, while those from dynamic backgrounds may enjoy either. Different
people learn differently! Choose whatever’s right for you.

[learnrust]: learn-rust.html
[syntax]: syntax-and-semantics.html