Learn to Code via Tutorials on Repl.it!

← Back to all posts
A Basic Introduction to Rust
liltaco (204)

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 Hello, 5.


Variables are declared using let, e.g. 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) - i8, i16, i32, i64.
  • Unsigned integers (can't be negative) - u8, u16, u32, u64.
  • Floating point - f32, f64.
  • char - A unicode character.
  • String - A string. &str is a string slice, which is used for efficiency in code.
  • bool, which is true and false.
  • (), which is the empty type. It's used a lot.
  • Arrays: [1, 2, 3]. Their length can't be changed.
  • Tuples: (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 Vec::new().

Getting input

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 while true.


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 range(100).

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:

  • Ownership.
  • Ownership!!!
  • Splitting code to multiple files (modules)
  • Cargo (using third party packages).
  • Traits.
  • Error handling.
  • Concurrency.
    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.
reedrainer (0)

Lovely sum up! Ill be bookmarking for future use :)

xxpertHacker (931)

How about &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.

EpicGamer007 (1794)

Dude, this is pretty awesome!

CodeLongAndPros (1645)

It's correcthorsebatterystaple I think, the capitalization removes entropy.

lisa2006x (12)

Oh that nice! 😊