컨트리뷰션아카데미의그림자 - Cargo 기초
Cargo
Cargo는 rust의 패키지 매니저이다. java의 maven, node.js의 npm의 위치에 있다.
기본 흐름
cargo new helloworld
로 hello, world가 출력되는 콘솔 프로그램을 생성한다.
cargo build
로 빌드한 후에
cargo run
로 실행하면
hello, world!
가 출력된다.
바이너리는
/target/debug
에 생성되며
바이너리를 바로 실행해도 된다.
% target/debug/helloworldHello, world!
패키지 매니징
패키지 관련 정보는 Cargo.toml에 기록된다.
의존성이 있는 패키지는 Cargo.toml파일의 [dependencies]하단에 기록 후
[dependencies]ferris-says = "0.2"# https://crates.io/crates/ferris-says
빌드하면
cargo build
빌드 시점에서 없는 의존성 패키지를 다 가지고 온다(fetching).
패키지를 이용한 간단 프로그램
ferrirs_says는 ferrirs라는 캐릭터에 말풍선으로 입력된 문자열을 보여주는 라이브러리다. 디펜던시를 추가했으니 라이브러리를 이용해서 간단한 프로그램을 짜고
use ferris_says::say; // from the previous stepuse std::io::{stdout, BufWriter};fn main() {let stdout = stdout();let message = String::from("Hello fellow Rustaceans!");let width = message.chars().count();let mut writer = BufWriter::new(stdout.lock());say(message.as_bytes(), width, &mut writer).unwrap();}
돌리면
cargo run
잘 나온다
__________________________< Hello fellow Rustaceans! >--------------------------\\_~^~^~_\) / o o \ (/'_ - _'/ '-----' \