Rust for the Novice Programmer/Hello World
Hello World!
editIt is a tradition in programming that the first program people see is one that prints 'Hello World'.
Make a folder to store your learning exercises in and inside make a folder. You can call it anything you want but an example name might be 'hello_world'.
- Open the terminal inside this folder.
- Type 'cargo init'
- You should notice the following things inside the folder:
- .git (you might not see this if you have hidden items turned off)
- .gitignore
- Cargo.toml
- src folder, inside which is main.rs
- For now we will only concern ourselves with main.rs which is inside src so open this in your text editor.
Inside you will see:
fn main() {
println!("Hello, world!");
}
What does this mean?
- 'fn' is short for 'function'. We will revisit this later, for now just know it is a collection of code.
- 'fn main()' means it is a function called 'main'.
- The main function is special as it is the start point of your program.
- The braces { and } indicate the beginning and end of the function.
- Then the line 'println!("Hello, world!");' is made up of different parts:
- 'println!' is the name of the function being called. We don't need to know that much except the stuff between ( and ) will be printed to the terminal.
- Then the " " quotes around "Hello, world!" indicates the stuff inside is some text.
To run this, type 'cargo run' in the terminal.
You should see the lines showing the code being compiled, then the text
Hello, world!
being printed to the terminal!
You can try changing the text within the "" quotes to see different messages being printed!