Hello World!
Our first rust program
To get started we are going to use the
cargo new
command. This will generate all of the required project files and put them in the right place.
$ cargo new hello_world
Created binary (application) `hello_world` project
$ cd hello_world
$ tree
.
├── Cargo.toml
└── src
└── main.rs
We get a new
Cargo.toml
file, a src
folder and in that a main.rs
file.
main.rs
fn main() {
println!("Hello, world!");
}
Like
C
programs, rust programs require a function called main
. For a rust program to work main
needs to take no parameters and return nothing (there are a few things it can return but let's just stick with this for now). Here we have a function that simply prints "Hello, world!" to the command prompt; lets test that out. To do that we are going to use the command cargo run
, which will compile our program and then run it, the program will be compiled into a ./target/debug/
folder if you wanted to dig it up later.
$ cargo run
Compiling hello_world v0.1.0 (file:///mnt/c/Users/rmasen/Documents/projects/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 2.56 secs
Running `target/debug/hello_world`
Hello, world!
It worked!