Installing Rust


Create new project:


  • In your terminal run these commands:
cargo new your_bevy_prj_name
cd your_bevy_prj_name
  • After running above commmands, your project folder will have a tree structure like this:
│   Cargo.lock
│   Cargo.toml
├───src
|      main.rs
└───target
  • main.rs is your Root Crate which is the entry point of the program:
fn main() {
    println!("Hello, world!");
}
  • Cargo.toml is the “project file” which includes:
    • Metadata
    • Dependencies
    • Build configs
[package]
name = "your_bevy_prj_name"
version = "0.1.0"
edition = "2021"
 
[dependencies]

Add Bevy to the project:


  • Method 1: cargo add bevy.
  • Method 2: Add the following line bevy = "*" to your toml file:
[package]
name = "your_bevy_prj_name"
version = "0.1.0"
edition = "2021"
 
[dependencies]
bevy = "*"
  • * means it will get the latest version of bevy crate. If you want to pick a specific version go to this website and check the version Bevy.

Compiling optimization:


  • In case you want to debug the game, the complication is super slow for doing that. To optimize it, add these following lines in your toml file:
# Enable a small amount of optimization in debug mode
[profile.dev]
opt-level = 1
 
# Enable high optimizations for dependencies (incl. Bevy), but not for our code:
[profile.dev.package."*"]
opt-level = 3

Optional:


Feature:

  • Enable bevy’s dynamic linking Feature:
    • Run cargo run --features bevy/dynamic_linking each time.
    • If you don’t want to do it each run then add the following feature to your toml file:
[dependencies]
bevy = { version = "0.13.0", features = ["dynamic_linking"] }

Compiler:

  • Nightly Rust Compiler: Not stable but this gives lightly performance than the stable rust.
    • Create “rust-toolchain.toml” file
    • Then put following code:
[toolchain]
channel = "nightly"

Linker:

  • Choose one of following linkers:
  • Use LLD linker:
    • Windows:
cargo install -f cargo-binutils
rustup component add llvm-tools-preview
  • Use mold linker: x5 faster than LLD linker but not stable and not Windows support
    • Ubuntu: sudo apt-get install mold clang

Enable fast compiles:

  • Install both LLD linker and nightly rust compiler.
  • Put .cargo/config.toml next to src folder.
  • Copy following code in the url to config.toml file: link

Rebuild:

  • Build your project again to see the significant change.
  • The first time may be slow but after that it will be fast.