Hello, world! and Cargo
착한 반말 컨셉
Rust 튜토리얼
terminal
$ mkdir ~/projects
$ cd ~/projects
$ mkdir hello_world
$ cd hello_world
hello_world.rs
fn main() {
println!("Hello, world!");
}
output
$ rustc main.rs
$ ./main
Hello, world!
cargo 버젼 체크
$ cargo --version
새로운 프로젝트 생성
$ cargo new hello_cargo
$ cd hello_cargo
cargo.toml 파일 구성
[package]
name = "hello_cargo"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
edition = "2018"
[dependencies]
note:
editionkey in Appendix E. -> 여기서 toml 파일 설정에 대해 좀더 알아볼 예정.
- dependencies 는 rust 에서 crates 라는 이름으로 불림. (packages of code are referred to as crates)
- The top-level project directory is just for README files, license information, configuration files, and anything else not related to your code.
- 가장 탑레벨의 프로젝트 디렉토리에는 README 파일, 라이센스 정보, 설정 파일 등이 들어가.
이제 프로젝트 폴더에서 <폴더이름>/hello_cargo
cargo build 를 하면 이렇게 나올거야.
$ cargo build
Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 2.85 secs
실행 할때는 폴더명이 좀 다른데, 현재 폴더에서 target/debug/ 아래에 실행 파일이 있을거야.
$ ./target/debug/hello_cargo # or .\target\debug\hello_cargo.exe on Windows
Hello, world!
cargo build 후에 실행 파일을 실행하는 방법도 있지만, cargo run 으로 한번에 실행하는 방식도 있어.
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
Running `target/debug/hello_cargo`
Hello, world!
cargo check 라는 커맨드는 컴파일도 하지않고 그냥 코드가 제대로 잘 작성되었는지 체크하는 커맨드야.
언제 쓰냐고? 보통 cargo build 보다 cargo check 가 훨씬 빠르기 때문에, 중간 중간 체크하기 위해서 사용될수 있어.
실행할 준비가 되었다고 생각하면 cargo build 를 통해 컴파일해보고 실행하면 돼!
cargo 를 사용하면서 부터는 어떤 운영체제든 상관없이 일관된 커맨드를 사용하기때문에 헷갈릴 일도 없지.
Release 할때는 cargo build --release 커맨드를 사용해.
target/debug 대신 target/release 폴더에 파일을 생성하지.
컴파일은 느리게 되지만 빠른 실행이 될수 있도록 도와줘.
이미 존재하는 프로젝트를 카피해서 빌드해보고 싶다면, 아래 커맨드를 입력하면돼.
$ git clone example.org/someproject
$ cd someproject
$ cargo build
주의
챕터 2로 넘어가기 전에, 좀더 기초를 쌓고 프로젝트를 해보고 싶은 사람들(나를 포함)은 챕터 3를 먼저 하는걸 추천할게.
Chapter 3
다음 페이지에서 계속…