Rust

RUST Documentation 따라 공부하기 1

2019.12.30


Getting Started "RUST"

Local documentation : rustup doc

  1. hello_rust & 2. hello_cargo

Hello World!

// main.rs
fn main() {
  println!("Hello world!");
}
rustc main.rs
  • "main" function is program's entry point.

Cargo

rust는 오직 rustc 명령어로 컴파일한다.. 이 작업을 모든 rs 파일에 대해서 진행한다? 그럴 수 없다

Cargo is Rust’s build system and package manager. Most Rustaceans use this tool to manage their Rust projects because Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries. (We call libraries your code needs dependencies.)

cargo new hello_cargo
  • cargo.toml

toml : Tom's Obvious, Minimal Language

: Cargo's configuration format

# Cargo.toml

[package]
name = "hello_cargo"
version = "0.1.0"
authors = ["iseongbin <lsb918@uos.ac.kr>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

The last line, [dependencies], is the start of a section for you to list any of your project’s dependencies. In Rust, packages of code are referred to as crates.

Cargo expects your source files to live inside the src directory. The top-level project directory is just for README files, license information, configuration files, and anything else not related to your code. Using Cargo helps you organize your projects. There’s a place for everything, and everything is in its place.

# build project
cargo build

# how to run?
./target/debug/hello_cargo
# or just
cargo run
# building for release
cargo build --release

# to check code to make sure it compiles, but doesn't produce executable.
# Rust는 빌드 속도가 느리므로,
# cargo check를 통해서 파일이 컴파일 되는지를 빠르게 확인함으로써
# 프로젝트의 진행속도를 높일 수 있다.
cargo check
  • Cargo.lock file keeps track of the exact dependencies of the project.