Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Getting started with rust-toolset

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

    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 <jistone@redhat.com>"]
    
    [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

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.