[Rust] Rust Dev Install Guide (feat. VS Code)

kimh4nkyul·2023년 2월 16일
0

Installation

Linux, Mac에서 Rust 설치

curl https://sh.rustup.rs -sSf | sh

Env 설정

vim ~/.bash_profile (또는 .bashrc) 
export PATH=$HOME/.cargo/env
source ~/.bash_profile (또는 .bashrc)

#(zsh의 경우)
vim ~/.zshenv
. "$HOME/.cargo/env"
source ~/.zshenv

VS Code 설정

rust-analyzer

VS Code(Visual Studio Code) 에서는 마켓플레이스에서 rust-analyzer 익스텐션을 설치한다.

원래는 Rust 익스텐션을 지원 했었지만 현재 공식적으로 deprecated 된 상태이다.

rust-analyzer 는 다음과 같은 기능을 VS Code에서 지원한다.

Features

설치가 완료되면 아래 그림과 같이 rust-analyzer 를 클릭해 실행시키고 작업하면 된다.

동그란 원 안에 사각형 버튼이 나온다면 analyzer가 중지 상태이고,

없다면 analyzer가 실행 상태인 것으로 VS Code에서 문법과 관련된 기능이나 실행, 디버깅, 인텔리센스, 에러 등을 보여줄 것이다.

👋 참고로 IntelliJ IDEA에서는 Rust 플러그인을 설치하면 Rust Language를 지원한다.

Debug

VS Code에서 러스트를 디버깅하기 위해선 CodeLLDB 또는 MS C++ Tools 익스텐션이 필요하다.

여기서는 CodeLLDB 를 설치했다.

Hello, World!

Basic Sample

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

hello_world 디렉토리 생성 후, hello_world.rs 에 위와 같은 코드를 추가하고,

터미널에서

rustc hello_world.rs

입력하면 실행 파일이 생성된다.

Cargo Sample

터미널에서

#(cargo new로 package 생성, **바이너리 프로그램을 만들거라면 --bin/라이브러리는 --lib**) 
cargo new hello_cargo --bin 
#(생성 완료 메시지)
Created binary (application) `hello_cargo` package 

입력하면 아래와 같은 구조로 패키지가 생성된다.

hello_cargo             #(최상위 디렉토리)
├── Cargo.lock          #(의존성 추적, Cargo가 관리) 
├── Cargo.toml          #(패키지 환경설정 및 의존성 관리)
├── src                 #(러스트 소스코드 디렉토리)
│   └── main.rs
└── target              #(빌드 정보 및 실행 파일 관리)
    ├── CACHEDIR.TAG
    └── debug           #(빌드 시 실행 파일 위치)
        ├── build
        ├── deps
        │   ├── hello_cargo-49c6e585f9e2601e.d
        │   ├── hello_cargo-8e0cc8f8f6dfa190.d
        │   ├── libhello_cargo-49c6e585f9e2601e.rmeta
        │   └── libhello_cargo-8e0cc8f8f6dfa190.rmeta
        ├── examples
        └── incremental
            ├── hello_cargo-2eiw9xf3thulm
            │   ├── s-gfwy24z7nh-joc5oh-3ra520mdg355a
            │   │   ├── dep-graph.bin
            │   │   ├── query-cache.bin
            │   │   └── work-products.bin
            │   └── s-gfwy24z7nh-joc5oh.lock
            └── hello_cargo-dlerqzlsenno
                ├── s-gfwy24z7nf-11a7sb6-2big7iv1zezu
                │   ├── dep-graph.bin
                │   ├── query-cache.bin
                │   └── work-products.bin
                └── s-gfwy24z7nf-11a7sb6.lock

TOML을 이용한 의존성 관리

Rust에서 의존성은 프로젝트의 루트에 위치하는 Cargo.toml 파일에서 할 수 있다.

어떤 의존성을 추가할 수 있는지는 Rust Pacakge Registry 인 Crates 에서 확인 가능하다.

https://crates.io/

추가할 의존성이 있다면, TOML 파일에서 [dependencies] 아래에 다음과 같이 문구를 추가한다.

[dependencies] 
rand = "0.7.3" 

프로젝트 빌드

cargo check

#(프로젝트가 빌드 될 수 있는지 체크, 실행 파일 생성 과정이 생략되어 빠름)
cargo check 

cargo build

#(프로젝트 빌드, 빌드 후 target/debug에 실행 파일 생성)
cargo build

#(빌드 후 패키지 구조) 
hello_cargo
├── Cargo.lock
├── Cargo.toml
├── src
│   └── main.rs
└── target
    ├── CACHEDIR.TAG
    └── debug
        ├── build
        ├── deps
        │   ├── hello_cargo-49c6e585f9e2601e.d
        │   ├── hello_cargo-502e728374a85b47
        │   ├── hello_cargo-502e728374a85b47.1m3frmwl83rm460w.rcgu.o
        │   ├── hello_cargo-502e728374a85b47.1v3hl1urcmgchy3q.rcgu.o
        │   ├── hello_cargo-502e728374a85b47.42x0vqhb8ie64dqs.rcgu.o
        │   ├── hello_cargo-502e728374a85b47.48qej5gg7rgkd015.rcgu.o
        │   ├── hello_cargo-502e728374a85b47.5gc8vcesdkdk8pxo.rcgu.o
        │   ├── hello_cargo-502e728374a85b47.cm98o5rwo5yzd8l.rcgu.o
        │   ├── hello_cargo-502e728374a85b47.d
        │   ├── hello_cargo-8e0cc8f8f6dfa190.d
        │   ├── libhello_cargo-49c6e585f9e2601e.rmeta
        │   └── libhello_cargo-8e0cc8f8f6dfa190.rmeta
        ├── examples
        ├── hello_cargo           #(실행 파일)
        ├── hello_cargo.d
        └── incremental
            ├── hello_cargo-2eiw9xf3thulm
            │   ├── s-gfwy24z7nh-joc5oh-3ra520mdg355a
            │   │   ├── dep-graph.bin
            │   │   ├── query-cache.bin
            │   │   └── work-products.bin
            │   └── s-gfwy24z7nh-joc5oh.lock
            ├── hello_cargo-dlerqzlsenno
            │   ├── s-gfwy24z7nf-11a7sb6-2big7iv1zezu
            │   │   ├── dep-graph.bin
            │   │   ├── query-cache.bin
            │   │   └── work-products.bin
            │   └── s-gfwy24z7nf-11a7sb6.lock
            └── hello_cargo-qqq72hiizrca
                ├── s-gfwyrnalby-1cpnau2-2a0z3ac1e6y8h
                │   ├── 1m3frmwl83rm460w.o
                │   ├── 1v3hl1urcmgchy3q.o
                │   ├── 42x0vqhb8ie64dqs.o
                │   ├── 48qej5gg7rgkd015.o
                │   ├── 5gc8vcesdkdk8pxo.o
                │   ├── cm98o5rwo5yzd8l.o
                │   ├── dep-graph.bin
                │   ├── query-cache.bin
                │   └── work-products.bin
                └── s-gfwyrnalby-1cpnau2.lock

cargo run

#(실행 파일 RUN, 프로젝트가 수정 되었다면 빌드도 수행)
cargo run 

Cargo Expand

intro

cargo expand 는 러스트에서 사용할 수 있는 매크로들을 확장 출력해주는 툴이다.

다음과 같은 코드가 있다면

cargo expand 는 아래 그림과 같이 매크로인 println!(..) 를 확장해 보여줄 것이다.

install

cargo-expandcargo install cargo-expand 로 설치할 수 있다.

글로벌로 설치가 되기 때문에 cargo 명령을 이용할 수 있는 곳이라면 어디든 사용할 수 있다.

nightly version (*Nightly: 대부분의 개발이 이루어지고 있는 마스터 브랜치의 최신 빌드 버전)이기 때문에버전에 맞는 툴체인을 설치해 줘야 한다.

# (명령 입력)
rustup toolchain list
# (출력문)
stable-aarch64-apple-darwin (default)

rustup toolchain list 로 현재 설치되어 있는 툴체인을 확인한다. (PC 환경 마다 다르기 때문에)

그리고 rustup toolchain install nightly-aarch64-apple-darwin 명령으로 Nightly 툴체인을 설치한다. Mac M1 Apple Silicon 기준이므로 반드시 본인 PC 환경에 맞게 설치해야 한다.

문법

그 외 읽을거리

profile
Hi there.

0개의 댓글