Introduction to Rust for Android Developers

Original tutorial linkComprehensive Rust

Introduction to Rust for Android Developers

This is a free Rust tutorial developed by the Google Android team. The tutorial comprehensively covers all aspects of Rust, from basic syntax to advanced topics such as generics and error handling.

The goal of this tutorial is to teach the Rust programming language. We assume you have no prior knowledge of Rust and aim to achieve the following objectives:

  • • A comprehensive understanding of Rust syntax and language features.
  • • The ability to modify existing Rust programs and write new Rust programs.
  • • Learning common Rust programming idioms.

Non-Goals of the Tutorial

Rust is a rich language, and we cannot cover everything in just a few days. Some non-goals of this tutorial include:

  • • Learning how to develop macros: please refer to “The Rust Programming Language” and “Rust by Example” for related content.

Macros in Rust are a complex concept that requires separate discussion. Rock Byte recommends a book titled “Write Powerful Rust Macros” for further reading.

Assumptions of the Tutorial

This tutorial assumes you have programming knowledge. As the title suggests, it is an introductory tutorial for Android developers, so readers should have a basic understanding of programming.Rust is a statically typed language, and we sometimes compare it with C and C++ to better explain or contrast how Rust handles certain tasks.

Installing Cargo

Introduction to Rust for Android DevelopersWhen we start reading about Rust, we will quickly encounter Cargo, which is the standard tool for building and running Rust applications in the Rust ecosystem. Here, we want to briefly introduce what Cargo is, how it fits into the broader ecosystem, and how it integrates into this training.

Please refer to this link to install the necessary tools, which will also install the Cargo build tool (<span>cargo</span>) and the Rust compiler (<span>rustc</span>). Additionally, it will install <span>rustup</span>, a command-line tool for installing different versions of the compiler.

After installing Rust, you should configure your editor or IDE to support Rust development. Most editors achieve this by communicating with <span>rust-analyzer</span>, which provides autocompletion and go-to-definition features for VS Code, Emacs, Vim/Neovim, and others. Additionally, there is another IDE called RustRover available. Of course, for short Rust code snippets, the official online Playground is provided, where you can run some short code and share your code snippets.

If you prefer not to deal with an IDE, I recommend using RustRover.

The Rust Ecosystem

Rust ecosystem consists of many tools, with the main tools including:

  • <span>rustc</span>: Rust compiler that converts <span>.rs</span> files into binaries and other intermediate formats.
  • <span>cargo</span>: Rust‘s dependency manager and build tool. Cargo knows how to download dependencies, typically hosted on <span>https://crates.io</span>, and passes them to <span>rustc</span> during project builds. Cargo also comes with a built-in test runner for executing unit tests.
  • <span>rustup</span>: Rust toolchain installer and updater. This tool is used to install and update <span>rustc</span> and <span>cargo</span>, and when new Rust versions are released. Additionally, <span>rustup</span> can download documentation for the standard library. You can install multiple versions of Rust, and <span>rustup</span> allows you to switch between different versions as needed.

In this tutorial, we will provide example code in the text, but it will not run immediately, so readers can use the online Playground to run our code. Nevertheless, we still recommend installing Cargo: this will make it easier for readers to complete the exercises. In the final part of the tutorial, we will conduct a larger exercise demonstrating how to handle dependencies, which will require Cargo.

Learning Rust will definitely involve installing Cargo, just as learning Android requires having Gradle.

Running Locally

All code in this tutorial can be run locally. After you have fully installed Rust as described in the previous sections, you can follow these steps to build a runnable Rust program (referred to as a binary project in Rust):

  1. 1. Copy the example code you want to run.
  2. 2. Use <span>cargo new exercise</span> to create a new <span>exercise/</span> directory to store your code.
  3. 3. Navigate to the <span>exercise/</span> directory and use <span>cargo run</span> to build and run your binary.
  4. 4. Replace the boilerplate code in <span>src/main.rs</span> with the code you copied in step 1.
  5. 5. Use <span>cargo run</span> to build and run the program.
  6. 6. Use <span>cargo check</span> to quickly check your project for errors, and use <span>cargo build</span> to compile it without running. You will find the output of the normal debug build in <span>target/debug/</span>. Use <span>cargo build --release</span> to generate an optimized <span>release</span> version build in <span>target/release/</span> (the <span>release</span> version is usually faster).
  7. 7. You can add dependencies to your project by editing <span>Cargo.toml</span>. When you run <span>cargo</span> commands, it will automatically download and compile any missing dependencies (similar to how Gradle works).

Conclusion

The introduction is complete, and we will now dive into the actual content of our tutorial.

Leave a Comment