As per usual, we’ll create a simple “Hello World” style app to create our first program in Rust.
While you can download and install a Rust compiler on your machine, for our examples, we will use an online editor: https://www.onlinegdb.com/online_rust_compiler
Rust requires a main function, like many other languages (C/C++, Java, etc) however, there are minor variations from those. For example, to define a function we use the fn command, unlike def
for Python or Rust, or function
in JavaScript, for func
in Go.
fn main() {
}
Notice how it has an empty parameter list, and braces to define the function like you’d see in C/C++, Java, JavaScript and other languages.
Within the main function, we need to print something to the console. We can do that with the println
command.
fn main() {
println!("Hello, world!");
}
Now technically, println
is not a function. It is a macro. And that is why it needs to have a !
after the name.
Your First Rust Program was originally found on Access 2 Learn