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

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.

主站蜘蛛池模板: 合水县| 罗源县| 隆德县| 福泉市| 天峨县| 余干县| 集安市| 南和县| 南宁市| 安图县| 格尔木市| 雷州市| 花莲县| 丰城市| 宣威市| 阿拉善盟| 新闻| 卢氏县| 屏南县| 册亨县| 延庆县| 宜州市| 南江县| 马关县| 武定县| 江孜县| 祁连县| 宁夏| 钟祥市| 广昌县| 神农架林区| 芦山县| 濮阳市| 武冈市| 武城县| 通化县| 泗水县| 平江县| 微山县| 密山市| 广宗县|