官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 晋中市| 永年县| 梅州市| 兴仁县| 化州市| 兴国县| 吉首市| 胶南市| 宣武区| 惠州市| 安岳县| 镶黄旗| 岑巩县| 辽阳市| 东莞市| 塘沽区| 响水县| 泉州市| 贞丰县| 砚山县| 黄陵县| 锡林郭勒盟| 枣强县| 崇义县| 泰来县| 贺州市| 四会市| 凤阳县| 紫金县| 柘城县| 永城市| 通许县| 伊宁市| 云安县| 临汾市| 西乌珠穆沁旗| 泗洪县| 金平| 拉萨市| 彭州市| 永安市|