Files
rust-examples/Writerside/topics/starter-topic.md
2024-06-21 14:47:31 +09:00

1.5 KiB

Rust

rustup

먼저, 러스트 툴체인 인스톨러를 설치합니다.

sudo snap install rustup --classic
rustup -V # 버전 확인

RustUp을 통해서 Rust를 설치합니다.

rustup install stable
rustup default stable

또는

$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh

프로젝트 생성

mkdir my_project
cd my_project
cargo init

또는,

cargo new my_project

위 명령을 실행하면 프로젝트 폴더에 'Cargo.toml' 과 'src/main.rs' 등의 파일이 생성됩니다.

Hello, world!

위에서 생성된 main.rs 파일은 간단한 헬로 월드 프로그램입니다.

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

println은 화면에 한 줄 출력하는 매크로입니다. 러스트에서는 매크로 이름 뒤에 !가 붙습니다.

cargo build

./target/debug 디렉토리에 실행 파일이 만들어집니다. 다음 명령으로 프로그램을 실행시키면 화면에 'Hello, world!'가 출력됩니다.

./target/debug/my_project

배포용으로 컴파일하려면, release 옵션을 추가합니다.

cargo build --release

cargo run 명령을 사용하면 빌드와 동시에 실행할 수 있습니다.

cargo run

또는,

cargo run --release

rustfmt

소스 코드를 깔끔하게 정리해 줍니다.

rustfmt ./src/main.rs

또는, 아래 명령으로 전체 소스 파일에 대해 처리할 수 있습니다.

cargo fmt