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

Test your installation

Let's try to build a Rust program. First, create a new project with cargo:

$ cargo new --bin hello_world
     Created binary (application) `hello_world` project

The --bin flag indicates that we want to create an executable project, as opposed to a library (which is the default without this flag). In the Rust world, a crate is a package of libraries and/or executable binaries.

This created a hello_world directory containing the following files and directory:

$ tree hello_world/
hello_world/
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files

The Cargo.toml file is where the metadata (name, version, and so on) of your project resides, as well as its dependencies. The source files of your project are in the src directory. It's now time to run this project:

$ cd hello_world/
$ cargo run
   Compiling hello_world v0.1.0 (file:///home/packtpub/projects/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.39 secs
     Running `target/debug/hello_world`
Hello, world!

The first three lines printed after cargo run are lines printed by cargo indicating what it did: it compiled the project and ran it. The last line, Hello, world!, is the line printed by our project. As you can see, cargo generates a Rust file that prints text to stdout (standard output):

$ cat src/main.rs
fn main() {
    println!("Hello, world!");
}

If you only want to compile the project without running it, type the following instead:

$ cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs

This time, we didn't see Compiling hello_world because cargo did not see any changes to the project's files, thus, there's no need to compile again.

主站蜘蛛池模板: 陕西省| 山丹县| 汝州市| 永仁县| 桃源县| 榆社县| 石台县| 金川县| 正蓝旗| 双辽市| 永仁县| 舒城县| 元氏县| 文登市| 镇坪县| 常熟市| 巨野县| 永靖县| 宝丰县| 石渠县| 巨鹿县| 兰溪市| 寿宁县| 内江市| 互助| 仙居县| 辽中县| 仙桃市| 洪湖市| 凤庆县| 华坪县| 广丰县| 姚安县| 开平市| 宁国市| 晋城| 出国| 临夏县| 衡东县| 肇州县| 蒲江县|