Rust
An Introduction
Donald Robertson 2022-06-01
About Me
- I am a final year PhD student working on network transports and streaming media
- I am also a professional software engineer with approximately 7 years of industry experience
- I have been programming in Rust as a hobby for around 6 years
- I am not an expert on programming languages
What is Rust?
- Rust is a
systemsgeneral purpose programming language - The official website describes Rust as 'a language empowering everyone to build reliable and efficient software'
- The language provides memory safety without the need for manual memory management or execution-time overheads like a runtime or garbage collection
A Brief Timeline
- Publicly announced at the Mozilla Annual Summit in 2010
- Reached version 1.0.0 in May 2015
- Rust is named the most loved programming language by the annual Stack Overflow developer survey for the first time in 2016
- Rust v1.31 is released marking the first new "edition" of the language since v1.0.0 in December 2018
- The Rust Foundation is announced in February 2021
- Rust v1.56 is released as the most recent edition of the language in October 2021
Language Features
Memory Safety
- Ownership and borrowing
- Rules are enforced at compile-time
- Guarantees the absence of null pointers, dangling pointers (use-after-free), iterator invalidation and data races
- Avoids the need for runtime overheads such as a language runtime or garbage collection
Language Features
Type System
- Separation of data and behaviour
- Type inference but explicit mutability
- Generics and monomorphization
- Composition over inheritance
Language Features
Macros, FFI and Unsafe
- Macros provide metaprogramming capabilities
- FFI provides bi-directional interop with other languages
- Unsafe allows some of the safety checks to be disabled
Personal Experience
- Rust does more than any language I have used to guide you towards correctness
- Ecosystem is increasingly broad and generally high quality
- Community is incredibly welcoming and enthusiastic
Questions?
Installation
For Linux and macOS
- Rust is often available via system package managers, however the version they provide is not always up to date which can cause issues when developing or installing existing tools from source
-
The recommended way to install Rust is via
rustup
by running the below command and selecting the default installation option when prompted:$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Installation
For Windows and Other Platforms
-
For users of Windows the recommended method to install Rust is still via
rustup
, but the means of obtainingrustup
is different -
The installer may be obtained by visiting the
rustup
website and selecting the appropriate installer for your machine -
Alternatively, users of any architecture supported by Rust may download the appropriate
rustup
installer from those available in the official documentation
Development Environment
Rust Analyzer + ...
-
rust-analyzer
implements the Language Server Protocol (LSP) for Rust to enable IDE-like features in popular editors that support LSP -
Enabling integration with various popular editors is described in the
rust-analyzer
documentation
Development Environment
JetBrains IDEs
- Users of the JetBrains suite of IDEs should instead install IntelliJ Rust
- IntelliJ Rust adds language support and enables common IDE features including code completion to be used with Rust while providing integration with Cargo and a test runner
- For maximum support, the CLion IDE should be used, CLion includes a Rust-compatible debugger, CPU profiler and Valgrind memcheck for Rust
Coding Exercise
A Guessing Game
- The Rust Programming Language, commonly referred to as "The Book", is a great introduction to many features of the language (and is also available in print)
- The second chapter provides a walkthrough of programming a guessing game in Rust
-
The complete example includes use of the
cargo
command-line tool, handling input and writing output using the standard library and using a third-party crate to include extra functionality
Example Website
- The increasingly rich ecosystem of packages available from crates.io makes it easy to be productive in a number of domains
- An example of this is in the area of web development, an increasing number of crates provide functionality for everything from front-end development (via compilation to WebAssembly) to database and third-party API integrations
- These slides are hosted on a website written using only Rust, HTML and CSS, the code is available on GitHub (including the on-disk representation of these slides)
- Beyond this, Rust continues to gain traction for game development, command- line tooling, OS development, network protocol implementation, ...
Ecosystem and Tooling
-
cargo
and Crates - Rust-Analyzer and IntelliJ Rust
- Clippy and Rustfmt
Useful Resources
- The Book (I would highly encourage you to start here)
-
The Nomicon (for
those who like to live
dangerouslyunsafe
ly) - Rust User Forum
- Rust Playground
- Crates
- This Week in Rust
Summary
- Rust is a programming language with a focus on creating robust software without sacrificing performance
- Thank you for attending!