프로그래밍/RUST
[RUST] cargo 기본 사용법
chance
2024. 5. 14. 17:01
프로젝트 생성
새 폴더에 프로젝트 생성 및 초기화
cargo new project_name
cd project_name
기존 폴더에 프로젝트만 초기화
cargo init
생성시 인자
--bin : 바이너리 형태 배포, main.rs 생성됨
--lib : 라이브러리 형태 배포, lib.rs 생성됨
패키지 설정
패키지 설정을 위한 Manifest는 Cargo.toml 파일이며, 해당 파일을 직접 수정한다.
패키지
[package]
name = "패키지명"
version = "0.0.1"
authors = ["author1", "author2"]
edition = "2024"
description = "설명"
license = "라이센스"
repository = "git"
homepage = "homepage"
categories = ["category::type"]
keywords = ["키워드1", "키워드2"]
기능
특정 기능별 디펜던시 설정
[features]
default = []
feature1 = ["package", "feature2"]
feature2 = ["pacakge_name/feature_name"]
의존 패키지
패키지의 특정 기능만 사용하는 경우 해당 기능 배열 정의
[dependencies]
package = "version"
package = { version = "1", features = ["feature_name"] }
git_package = { git = "https://repository.git", branch="branch" }
git_package = { git = "https://repository.git", tag="tag" }
git_package = { git = "https://repository.git", rev="hash" }
path_package = { path = "../package/package_path" }
버전의 경우 아래와 같이 표현 가능
"^1.2" : 버전 요구사항
"~1.2" : 1.2.0 이상 ~ 1.3.0 미만
"1.*" : 1.0.0 이상 ~ 1.2.0 미만
다른 저장소 패키지
[dependencies]
some-create = { version = "1.0", registry="registry" }
레지스트리는 .cargo/config.toml 에 정의
플랫폼별 패키지
[target.'cfg(windows)'.dependencies]
.
.
[target.'cfg(target_arch="x86_64")'.dependencies]
.
.
개발용 의존 패키지
[dev-dependencies]
package = "version"
테스트코드, 샘플
샘플은
[[example]]
name = "샘플명"
required-features = ["feature"]
빌드
cargo build
옵션
Package
--package spec...
--workspace
--all
--exclude SPEC...
Target
--lib
--bin
--bins
--example name...
--examples
--test name...
--tests
--bench name...
--benches
--all-targets
Feature
--features features
--all-features
--no-default-features
Compilation
--target triple
--release
--profile name
--ignore-rust-version
--timings=fmts
Output
--target-dir directory
--out-dir directory
Display
--verbose
--quiet
--color auto|always|never
--message-format human|short|json|json-diagnostic-short|json-diagnostic-rendered-ansi|json-render-diagnostics
--build-plan
Manifest
--manifest-path path
--frozen
--locked
--offline
실행
cargo run
옵션
Package
--package spec
Target
--bin name
--example name
Feature
--features features
--all-features
--no-default-features
Compilation
--target triple
--release
--profile name
--ignore-rust-version
--timings=html|json
Output
--target-dir directory
Display
--verbose
--quiet
--color auto|always|never
--message-format human|short|json|json-diagnostic-short|json-diagnostic-rendered-ansi|json-render-diagnostics
Manifest
--manifest-path path
--frozen
--locked
--offline
샘플 실행
cargo run --release --example example_name