Rust Programming By Example
上QQ阅读APP看书,第一时间看更新

Setting up your Rust project

The Rust package manager, cargo, allows us to create a new project very easily with just one command, cargo new. Let's run it as follow:

 cargo new tetris --bin

You should have a new folder tetris containing the following:

     tetris/
     |
     |- Cargo.toml
     |- src/
         |
         |- main.rs

Note that if you ran cargo new without the --bin flag, then you will have a lib.rs file instead of main.rs.

Now write this into your Cargo.toml file:

    [package]
    name = "tetris"
    version = "0.0.1"

    [dependencies]
    sdl2 = "0.30.0"

Here, we declare that our project's name is tetris, its version is 0.0.1 (it isn't really important at the moment), and that it has a dependency on the sdl2 crate.

For the versioning, Cargo follows SemVer (Semantic Versioning). It works as follows:

[major].[minor].[path]

So here's exactly what every part means:

  • Update the [major] number version when you make incompatible API changes
  • Update the [minor] number version when adding functionalities that don't break backward compatibility
  • Update the [patch] number version when you make bug fixes that don't break backward compatibility

It's not vital to know this, but it's always nice to be aware of it in case you intend to write crates in the future.