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

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.

主站蜘蛛池模板: 潞城市| 应城市| 绥中县| 榆树市| 项城市| 阳泉市| 宁明县| 抚州市| 和硕县| 织金县| 治县。| 山西省| 博罗县| 凌源市| 启东市| 竹山县| 韩城市| 岢岚县| 新余市| 安远县| 无锡市| 海林市| 林甸县| 集安市| 集贤县| 克什克腾旗| 临沂市| 谢通门县| 板桥市| 临高县| 蒙自县| 从化市| 宝山区| 视频| 津市市| 东至县| 朝阳市| 达州市| 沛县| 郴州市| 固原市|