How to Install Rust on Ubuntu

A short tutorial to install Rust on Ubuntu. Rust runs at warp speed and guarantees thread safety. Rust uses a package manager called Cargo.

A friend asked me to check out Rust. I have heard of it but never looked into it much. Looking at the docs I see that it looks similar to C which I liked a while back in college.

Install Rust on Ubuntu

The first step is very easy:

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

This will show:

info: downloading installer
Welcome to Rust!
This will download and install the official compiler for the Rust programming  language, and its package manager, Cargo.
It will add the cargo, rustc, rustup and other commands to Cargo’s bin  directory, located at:
  /home/tom/.cargo/bin
This path will then be added to your PATH environment variable by modifying the
profile file located at:
  /home/tom/.profile
You can uninstall at any time with rustup self uninstall and these changes will
be reverted.
Current installation options:
  default host triple: x86_64-unknown-linux-gnu  default toolchain: stable  modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
1
info: syncing channel updates for ‘stable-x86_64-unknown-linux-gnu’
info: downloading component ‘rustc’  36.4 MiB /  36.4 MiB (100 %) 348.8 KiB/s ETA:  0 s  info: downloading component ‘rust-std’  50.0 MiB /  50.0 MiB (100 %) 400.0 KiB/s ETA:  0 s  info: downloading component ‘cargo’  4.7 MiB /  4.7 MiB (100 %) 384.0 KiB/s ETA:  0 s  info: installing component ‘rustc’
info: installing component ‘rust-std’
info: installing component ‘cargo’
info: default toolchain set to ‘stable’
  stable installed – rustc 1.16.0 (30cf806ef 2017-03-10)
Rust is installed now. Great!
To get started you need Cargo’s bin directory in your PATH environment  variable. Next time you log in this will be done automatically.
To configure your current shell run source $HOME/.cargo/env

Check if Rust was installed

Restart the shell and run:

$ rustc --version

This will show:

rustc 1.16.0 (30cf806ef 2017-03-10)

Running Hello World the “learning” way

rust ubuntu hello world

As seen on the Rust docs. Here is a summary to understand Rust quickly.

Here is the long and organized way for running hello world.

Create a directory:

$ mkdir hello_world
$ cd hello_world

Create a Cargo config file. Cargo is the package manager.

$ vim Cargo.toml

Then add content such as:

[package]

 

name = "hello_world"
version = "0.0.1"
authors = ["Tom Ordonez <py@pythonloop.com>" ]

Create a src directory

$ mkdir src
$ cd src

Create the source file:

$ vim main.rs

Enter this code:

fn main() {
  println!("Hola world");
}

This is not a typo println!. In Rust this is called a macro. While println is your usual print function.

Go back to the root directory of hello_world.

$ cd ..

Build and Run Cargo

Run this:

$ cargo run

This will show:

Compiling hello_world v0.0.1 (file:///home/tom...
Finished dev [unoptimized + debuginfo] target(s) in 0.44 secs
  Running `target/debug/hello_world`
Hola world

You will also see that new files are created in the directory.

There is a file called Cargo.lock

[root]
name = "hello_world"
version = "0.0.1"

And there is a directory called target with these contents

$ cd target
debug
$ cd debug
$ build deps examples hello_world hello_world.d incremental native

Running Hello world the short way

Just run:

$ cargo new hola_world --bin
Created binary (application) `hola_world` project
$ cd hola_world

This new directory will create 3 things:

  • Cargo.toml
  • src
  • src/main.rs

The Cargo.toml has this content:

[package]
name = "hola_world"
version = "0.1.0"
authors = ["tom <py@pythonloop.com>"]

The src/main.rs file has this content:

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

Now run it with $ cargo run

If this was fun please grab the URL and share it. If you have questions or comments please add them below

Related Post

Go to the Previous Directory in Vim I use Vim. Vim is the most productive text editor if you know some vim commands When I open a file and I am done with it. I often want to go...
Export Data From Firebird Database to CSV File Background Some software applications use a database called Firebird. This is an open source database. If the software application is compiled you ca...
Installing Ruby on Ubuntu Installing Ruby on Ubuntu As seen on RVM... Go to the terminal in Ubuntu. Add the public key from RVM $ gpg --keyserver hkp://keys.gnupg.n...
Installing Hipchat on Ubuntu Tested on: Ubuntu 12.04 LTS Thinkpad T400, 2GB RAM Intel Core 2 Duo CPU P8600, 32bit Sources: Official hipchat linux instructions Ask U...

Leave a Reply