Rust, 쉽게 하자!

Rust 입문

[Rust] crate.io

바로크냥 2022. 10. 13. 22:17

1. crate.io

crate는 Rust의 라이브러리를 일컫는 용어입니다.

영어 단어 crate는 물건이나 병을 담는 상자를 의미합니다.

라이브러리의 기능과 느낌을 잘 살린 단어 선택입니다.

우리가 직접 crate를 만들 수 있습니다. 만든 crate를 혼자 사용할 수도 있지만 모두에게 공개할 수도 있습니다. 

그것을 공개하고 관리하는 곳이 crate.io입니다.

주소는 다음과 같습니다.

https://crate.io/

 

crate를 사용하는 구체적인 방법은 앞에서 설명했지만 여기서 다시 설명하겠습니다.

우리가 다른 사람이 이미 만들어 놓은 crate를 사용하고 싶다면 위 주소로 찾아가서 검색을 해보면 됩니다. 

아니면 터미널에서 다음 명령어를 사용해서 crate.io에서 찾아 오게 할 수도 있습니다.

 

실습 삼아 해보겠습니다. 서버에서 데이터를 받아 오기 위해 필요한 http 관련 crate를 찾아보겠습니다.

>cargo search http

http = "0.2.8"                     # A set of types for representing HTTP requests and responses. 
qiniu-http-client = "0.1.2"        # Qiniu HTTP Client for Rust
constellation-server = "1.14.1"    # Pluggable authoritative DNS server. Entries can be added & removed from an HTTP REST API.
http-server = "0.7.4"              # Simple and configurable command-line HTTP server
gTickCheckerRust = "0.1.2"         # TickChecker is a simple management tool for server cloud computing in any field to check whe…
tokio-http2 = "0.1.9"              # HTTP/1.1 Library (HTTP/2 coming soon) using Tokio Project (core, proto, service). Used with …
didkit-http = "0.2.0"              # HTTP server for Verifiable Credentials and Decentralized Identifiers.
http_desync_guardian = "0.1.3"     # HTTP/1.1 request analysis to prevent HTTP Desync attacks
hurl = "1.6.1"                     # Hurl, run and test HTTP requests
tiny_http_sccache = "0.0.0"        # Low level HTTP server library
... and 3147 crates more (use --limit N to see more)

터미널 화면을 그대로 복사해온 것입니다.

사용한 명령어는

cargo search http

입니다. 

여러 개의 결과가 출력되었습니다.

 

하나만 따로 떼어 와서 살펴보겠습니다.

http = "0.2.8"                     # A set of types for representing HTTP requests and responses. 

 

http = "0.2.8"는 crate의 이름과 버전입니다. 

# 이하는 이 crate에 대한 간단한 설명입니다.

http = "0.2.8" 부분을 Cargo.toml 파일의 [dependencies]에 붙여 넣으면 프로젝트에서 http 크레이트를 사용할 수 있습니다. 

 

프로젝트를 cargo build나  cargo run으로 빌드를 할 때 크레이트를 다운로드합니다.

 

IDE를 사용해서 코딩할 때 크레이트의 메서드가 자동 추천 항목에 안 보일 때가 있습니다.

이때 cargo build나  cargo run을 한 번 실행하고 나면 메서드가 나타납니다.

 

좀 더 편한 방법도 있습니다. 

cargo add 크레이트 이름

명령을 사용하면 Cargo.toml에 자동으로 등록되고 다운도 받습니다.

 

 
반응형