A Basic Introduction to Rust
A Basic Introduction to Rust
While there is no alternative to reading The Book, I'd like to show a bit of the syntax and basics of Rust here.
Here is a Hello World program in Rust:
The reason it's
println! and not
println is because it's a macro, but macros are far too advanced to be covered here. Rust macros allow you to write code that writes code.
You can print formatted strings, for example:
This will print
Variables are declared using
let x = 5;. However, don't be fooled! This is not a dynamically typed language, but the type is inferred. That means that Rust understood x should be an integer.
Normal rust variables defined with
let are immutable (you can't do
let x = 5; x = 6;). To make them mutable, use
let mut x = 5;.
Here's a list of common types in Rust:
- Signed integers (can be negative) -
- Unsigned integers (can't be negative) -
- Floating point -
char- A unicode character.
String- A string.
&stris a string slice, which is used for efficiency in code.
bool, which is
(), which is the empty type. It's used a lot.
[1, 2, 3]. Their length can't be changed.
(1, true, "hi"). Their length can't be changed. Their types are written like
(i32, bool, &str).
- Vector: A variable size array.
vec![1, 2, 3]or
This is overcomplicated in Rust for some reason, though it's similar to how it is in Java.
Let's explain this. The first line,
use std::io, is similar to how it works in Java: while you could have written
std::io::stdin(), this gives you a shortcut for the rest of your code.
.read_line(&mut guess) - giving a function
&mut gives it a mutable reference, so it can change your variables.
.expect("Failed to read line") - basic Rust error handling, which prints "Failed to read line" and stops the program.
Conditionals, loops, and such
Here is an example of an if expression (I'll explain that later):
In Rust, if you don't put a semicolon on the last line, the value is returned. You can also use the usual
return. Here is an example of an if expression returning a value:
Loop is like
For loops are very similar to Python - they only go over iterators.
0..100 is an iterator of all the numbers 0,...,99. It's similar to
You can also iterate over an array or a vector:
Here is an example of a function:
Probably the most important feature of Rust, this is what replaces Object Oriented Programming. Let's see an example:
This is a struct with two variables, x and y. Let's put an instance of it in a variable:
Now, let's add some functions to it:
A function that doesnt take
&self is static and doesn't have access to self. We can call them like this:
Enums allow us to make types which can store values of many different types, or just be used as flags.
This will be more obvious with an example:
There's much more to Rust than this, including:
- Splitting code to multiple files (modules)
- Cargo (using third party packages).
- Error handling.
I recommend reading the official book, The Rust Programming Language. It might be a bit of a difficult read, but it's the only way to really know Rust.
&mut input, and introducing std::Option, and how Rust takes the route of returning errors, more like Haskell, instead of say, C++?
Missed a method call here:
Otherwise it seems okay.