+ - 0:00:00
Notes for current slide
Notes for next slide

Getting started with Rust

This presentation will help you get Rust installed, demonstrate some neat tools, and show you "hello world"

1 / 10

Installing Rust

The best way to install Rust is with rustup, a Rust version manager. To install it type:

$ curl https://sh.rustup.rs -sSf | sh

To keep your rust up-to-date with the latest release of rust, type:

$ rustup update

To check which version of Rust you have type:

$ rustc --version
2 / 10

cargo

Cargo is a tool that helps you develop Rust programs. It does several things:

  • Runs tasks: cargo build (compile your app), cargo test (test your app), cargo run (run your app)
  • Start a project: cargo new, cargo init

Cargo is also the package manager for Rust. This means that you can use Cargo to install and manage bits of other people's code.

  • A program or library is called a "crate".
  • A package contains one or more crates.
  • You can find Crates on http://crates.io
  • You list the Crates you want to use in the Cargo.toml file
  • Your app keeps track of what crates you are using in the Cargo.lock file
3 / 10

Setting up a Project

Creating a new project is easy!

  1. Type cargo new --bin name-of-my-project
    • Use --lib if you are writing a library
  2. cd into the new name-of-my-project directory

This will create several files and folders for you automatically:

  • Cargo.toml: metadata about your project and its dependencies
  • .gitignore: ignores compiled files built by Rust
  • src/lib.rs or src/main.rs: where your Rust code goes
4 / 10

lib.rs vs main.rs

There are 2 main types of projects you can make in Rust: a library and not a library.

If you are writing a library, it means you intend for your code to be used in someone else's application as a crate or module. If you want to do this, you should use a lib.rs.

If you are writing a not library, it means that you'd like to write code that compiles into a binary that someone can run. If you want to do this, you need to use a main.rs. Inside the main.rs you should have a main function that looks like this:

fn main() {
// your app code goes here
}
5 / 10

rustfmt

The rustfmt tool integrates with Cargo and can format your code for you! To install it:

$ rustup component add rustfmt-preview

And to use it:

$ cargo fmt

Cargo will re-write your files with the newly formatted code.

6 / 10

Hello, world!

If you generate a new program:

$ cargo new hello-world
$ cd hello-world

Cargo will make src/main.rs a "Hello World" program for you!

fn main() {
println!("Hello, world!");
}

Remember, you can run it with cargo run:

$ cargo run
Compiling hello-world v0.1.0 (file:///C:/hello-world)
Finished dev [unoptimized + debuginfo] target(s) in 2.6 secs
Running `target\debug\hello-world.exe`
Hello, world!
7 / 10

Basic Syntax

If we look at "hello world":

fn main() {
println!("Hello, world!");
}
  • fn declares a function. In this case, our function is named main. main will be called when your program starts.
  • The empty parenthesis means that main takes no parameters.
  • Function bodies go in curly braces: {}
  • The println! macro (the ! means it's a macro) prints stuff to the screen!
  • Strings go inside double quotes ""
  • Lines end with a semicolon ;

Try adding another call to println! with a second message!

8 / 10

cargo check

If you want the compiler to check your work, but you don't plan on actually running your program, you can use cargo check. It's faster than cargo build:

$ cargo build
Compiling hello-world v0.1.0 (file:///C:/hello-world)
Finished dev [unoptimized + debuginfo] target(s) in 1.21 secs
$ cargo check
Compiling hello-world v0.1.0 (file:///C:/hello-world)
Finished dev [unoptimized + debuginfo] target(s) in 0.85 secs

For "hello world" that's not much, but on real projects it really adds up!

9 / 10

Learning More

There are other "Rust in ten slides" presentations too, but you may also find these resources useful!

10 / 10

Installing Rust

The best way to install Rust is with rustup, a Rust version manager. To install it type:

$ curl https://sh.rustup.rs -sSf | sh

To keep your rust up-to-date with the latest release of rust, type:

$ rustup update

To check which version of Rust you have type:

$ rustc --version
2 / 10
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow