- Mastering Rust
- Rahul Sharma Vesa Kaihlavirta
- 372字
- 2021-07-02 13:35:20
Creating a new Cargo project
The cargo new <name> command creates a new project name as a directory. We can get more context on any subcommand by adding a help flag between cargo and the subcommand. We can view documentation for the new subcommand by running cargo help new, as shown in the following screenshot:

By default, cargo new creates a binary project; the --lib parameter has to be used when creating a library project. Let's try it out by typing cargo new imgtool and taking a look at the directory structure it creates:

Cargo has created some starter files, Cargo.toml and src/main.rs, with the function main printing Hello World!. For binary crates (executables), Cargo creates a src/main.rs file and for library crates it creates, src/lib.rs under the src/ directory.
Cargo also initializes a Git repository for new projects with the usual defaults, like preventing the target directory from being checked in with a .gitignore file, and checking in the Cargo.lock file for binary crates and ignoring it in library crates. The default Version Control System (VCS) that's used is Git, which can be changed by passing the --vcs flag to Cargo ( --vcs hg for mercurial). Cargo as of now supports Git, hg (mercurial), pijul (a version control system written in Rust), and fossil. If we want to modify this default behavior, we can pass --vcs none to instruct Cargo to not configure any vcs when creating our project.
Let's take a look at Cargo.toml for the project imgtool that we created. This file defines your project's metadata and dependencies. It's also known as the project's manifest file:
[package]
name = "imgtool"
version = "0.1.0"
authors = ["creativcoders@gmail.com"]
edition = "2018"
[dependencies]
This is the minimal Cargo.toml manifest file needed for a new project. It uses the TOML configuration language, which stands for Tom's Obvious Minimal Language. It is a file format that was created by Tom Preston-Werner. It is reminiscent of standard .INI files, but adds several data types to it, which makes it an ideal modern format for configuration files and simpler than YAML or JSON. We'll keep this file minimal for now and add things to it later.
- The Complete Rust Programming Reference Guide
- MySQL 8從入門到精通(視頻教學版)
- Learn to Create WordPress Themes by Building 5 Projects
- PyTorch自然語言處理入門與實戰
- 三維圖形化C++趣味編程
- Hands-On JavaScript High Performance
- Mastering LibGDX Game Development
- Java加密與解密的藝術
- Android開發案例教程與項目實戰(在線實驗+在線自測)
- 微服務架構深度解析:原理、實踐與進階
- Java網絡編程核心技術詳解(視頻微課版)
- Spring Boot+MVC實戰指南
- 快速入門與進階:Creo 4·0全實例精講
- JavaScript程序設計:基礎·PHP·XML
- Python計算機視覺和自然語言處理