Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Getting started with rust-toolset

November 1, 2017
Josh Stone
Related topics:
LinuxRust
Related products:
Developer Tools

Share:

    See More

    One of the new software collections we’ve introduced this fall is for Rust, the programming language that aims for memory and thread safety without compromising performance. Dangling pointers and data races are caught at compile time, while still optimizing to fast native code without a language runtime!

    In rust-toolset-7, we’re including everything you need to start programming in Rust on Red Hat Enterprise Linux 7, in the familiar format of software collections. In this release, we’re shipping Rust 1.20 and its matching Cargo 0.21 - both as Tech Preview. (NOTE: The “-7” in our toolset name is to sync with the other collections now being released, devtoolset-7, go-toolset-7, and llvm-toolset-7.)

    Install rust-toolset-7

    The toolset is available in the rhel-7-server-devtools-rpms repo for RHEL 7 Server, or rhel-7-workstation-devtools-rpms for RHEL 7 Workstation. (If you don’t already have RHEL 7, Red Hat offers no-cost RHEL subscriptions for development use here.) You can use subscription manager to enable the repo, then yum to install it as usual.

    # subscription-manager repos --enable rhel-7-server-devtools-rpms
    # yum install rust-toolset-7

    This will install the rustc compiler, the standard library, and the cargo build tool. Once installed, you can verify the versions and the installed path:

    $ scl enable rust-toolset-7 'rustc -V'
    rustc 1.20.0
    $ scl enable rust-toolset-7 'cargo -V'
    cargo 0.21.1
    $ scl enable rust-toolset-7 'rustc --print sysroot'
    /opt/rh/rust-toolset-7/root/usr

    Running through scl for every command can be a nuisance, but it’s easy to start a shell with the appropriate paths enabled:

    $ scl enable rust-toolset-7 bash

    The rest of this post assumes such an enabled shell.

    Start with Hello World

    What would a programming language be without this universal greeting? This is so ingrained that the example is directly available to us here. Rust projects are managed with the cargo tool, so let’s create a new program:

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

    This will automatically create a new project manifest, hello/Cargo.toml:

    [package]
    name = "hello"
    version = "0.1.0"
    authors = ["Josh Stone <[email protected]>"]
    
    [dependencies]

    And a new source file, hello/src/main.rs:

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

    Feel free to edit this, and then build and run it with cargo:

    $ cd hello/
    $ cargo run
       Compiling hello v0.1.0 (file:///home/jistone/hello)
        Finished dev [unoptimized + debuginfo] target(s) in 0.18 secs
         Running `target/debug/hello`
    Hello, world!

    Try some external dependencies

    Rust libraries are called crates, hosted online at crates.io, and cargo will take care of downloading and building all of your dependencies automatically. Let’s create a quick project to generate a vector of random numbers, and then sort them. First, run “cargo new --bin sorter”, and then add a dependency on the rand crate in Cargo.toml:

    [dependencies]
    rand = "0.3"

    Then edit src/main.rs to look like this:

    extern crate rand;
    
    use rand::Rng;
    
    fn main() {
        let mut rng = rand::thread_rng();
        let mut numbers: Vec<i8> = rng.gen_iter().take(10).collect();
        println!("random numbers: {:?}", numbers);
    
        numbers.sort();
        println!("sorted numbers: {:?}", numbers);
    }

    Then build and run it:

    $ cargo run
        Updating registry `https://github.com/rust-lang/crates.io-index`
     Downloading rand v0.3.17
     Downloading libc v0.2.32
        Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
         Running `target/debug/sorter`
    random numbers: [-83, -69, -75, -72, -48, 12, 82, -30, -101, 106]
    sorted numbers: [-101, -83, -75, -72, -69, -48, -30, 12, 82, 106]

    That’s simple enough -- how about adding some parallelism? Rust has strong compile-time protections from data races, and there are many crates, which build parallel abstractions from this, so let’s try the sorting functions in rayon. First, add another dependency to Cargo.toml:

    [dependencies]
    rand = "0.3"
    rayon = "0.8"

    Then use it in src/main.rs:

    extern crate rand;
    extern crate rayon;
    
    use rand::Rng;
    use rayon::prelude::*;
    
    fn main() {
        let mut rng = rand::thread_rng();
        let mut numbers: Vec<i8> = rng.gen_iter().take(10).collect();
        println!("random numbers: {:?}", numbers);
    
        numbers.par_sort(); // now sorting in parallel threads!
        println!("sorted numbers: {:?}", numbers);
    }

    Then build and run it again:

    $ cargo run
        Updating registry `https://github.com/rust-lang/crates.io-index`
     Downloading rayon v0.8.2
     Downloading rayon-core v1.2.1
     Downloading num_cpus v1.7.0
     Downloading lazy_static v0.2.9
     Downloading coco v0.1.1
     Downloading futures v0.1.16
     Downloading either v1.3.0
     Downloading scopeguard v0.3.3
       Compiling either v1.3.0
       Compiling scopeguard v0.3.3
       Compiling rayon-core v1.2.1
       Compiling lazy_static v0.2.9
       Compiling futures v0.1.16
       Compiling num_cpus v1.7.0
       Compiling coco v0.1.1
       Compiling rayon v0.8.2
       Compiling sorter v0.1.0 (file:///home/jistone/sorter)
        Finished dev [unoptimized + debuginfo] target(s) in 6.90 secs
         Running `target/debug/sorter`
    random numbers: [120, -103, 88, -1, 102, -122, -125, -20, -70, -114]
    sorted numbers: [-125, -122, -114, -103, -70, -20, -1, 88, 102, 120]

    There are more transitive dependencies now, but we didn’t have to rebuild the two we already had. The result should not be surprising -- different random numbers, of course, but still sorted.

    We’ve just been running the default [unoptimized + debuginfo] build. If you want to start measuring performance, you should use “cargo run --release”, enabling optimizations. Of course, a tiny 10-item vector is not very interesting here. If I increase this program to generate 100 million items -- gen_iter().take(100_000_000) -- and remove the prints, then my 2-core/4-thread laptop runs the serial sort in 8.2 seconds, and the parallel sort in 3.8 seconds. Although this is a very casual benchmark, it’s still a nice speedup for almost no effort!

    Install a useful Rust program

    Now that you have a working Rust toolchain, perhaps you’d like a complete program written in Rust. You could look to rustc and cargo themselves, but I also recommend installing ripgrep. This is a recursive grep tool, similar to Ack or The Silver Searcher, but even faster!

    $ cargo install ripgrep
        Updating registry `https://github.com/rust-lang/crates.io-index`
     Downloading ripgrep v0.7.1
      Installing ripgrep v0.7.1
     Downloading grep v0.1.7
    [... and all other dependencies]
       Compiling termcolor v0.3.3
    [... and all other dependencies]
       Compiling ripgrep v0.7.1
        Finished release [optimized + debuginfo] target(s) in 114.67 secs
      Installing /home/jistone/.cargo/bin/rg

    Just add ~/.cargo/bin to your PATH and start searching!

    $ PATH="~/.cargo/bin:$PATH"
    $ rg 'extern crate'
    sorter/src/main.rs
    1:extern crate rand;
    2:extern crate rayon;

    Note: programs built with rust-toolset-7 do not have any runtime dependency, so you can freely run them without worrying about any scl enable.

    Further Reading

    For more information, especially if you want to learn more about programming in Rust to begin with, please visit these resources:

    • rust-toolset-7 online documentation
    • Documentation from the Rust Project
      • Also available in the rust-toolset-7-rust-doc package, which installs to /opt/rh/rust-toolset-7/root/usr/share/doc/rust/html/index.html
    • Cargo Documentation from crates.io
      • Also available in the rust-toolset-7-cargo-doc package, which installs to /opt/rh/rust-toolset-7/root/usr/share/doc/cargo/html/index.html

    Take advantage of your Red Hat Developers membership and download RHEL today at no cost.

    Last updated: September 27, 2024

    Recent Posts

    • Container starting and termination order in a pod

    • More Essential AI tutorials for Node.js Developers

    • How to run a fraud detection AI model on RHEL CVMs

    • How we use software provenance at Red Hat

    • Alternatives to creating bootc images from scratch

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue