Build and Run

You can run a rust program with "rustc {filename}". This will generate an executable that can be directly ran. An example of this running for hello-world is below:
hello world console output

If we are using cargo, we can instead initialize a project with the command "cargo init". This creates the cargo.toml file, as well as the .gitignore. The program can then be run with "cargo run".

cargo run console output


Its worth noting that in the screenshot above you can see the line "Finished dev [unoptimized + debuginfo]". Rust has excellent performance for its executables, but this can be improved further by running the command "cargo build release" then "cargo run release". This will have the compiler do optimizations and make the code run faster during runtime, but take longer to compile. This can be taken further with specifications Cargo.toml file.

toml on creation
toml with optimizations

In the right image we can see four lines were added to the Cargo.toml file. The first line "[profile.release]" specifies that the following changes are made to the release build of the program. Rust uses multiple codegen units to paralellize the compilation. This can miss optimizations, thus if we set that to 1 with the command "codegen-units = 1", more optimizations can be found and our runtime improves. This comes at a cost of our compile time which will take longer. The outcome is expected for the next specficication.

"lto = fat" is link time optimization. When this is set to fat, it attempts to make optimizations across all crates within the dependency graph. The expectation of doing this is a runtime improvement of 10-20%.

The final part of that list is "opt-level = "z"". Instead of improving runtime, this command looks to reduce the binary file size of the program after compilation. There are 5 options for this setting: 1, 2, 3, "s", and "z", all of which have optimizations but attempt to optimize more as you get closer to "z".

Simple programs

  Hello World program:                     String Concatenation:
hello world code       string-concatenation-code

The hello world program is pretty simple as always. It doesn't require as much boilerplate as java, but is not as simple as python.

Since we already had the sum program for rust in our pub repository, we made a string concatenation version of it instead. This would take an array of strings and connect them into one string with a space between each work. Very similar to the sum program but with a different function.

Links to Other Simple Program

Hello World
Loop Examples
String Concatenation
User Input
Shopping Cart