## Overview

Rust is a modern systems programming language focused on performance and safety,
particularly memory safety. Rust is increasingly important in the Android
ecosystem as a safer alternative to C/C++ for developing critical system
components, offering improved security and stability.

This page guides you through setting up Android Studio for Platform (ASfP) to
support Rust development within your Android Open Source Project (AOSP)
projects.

## Prerequisites

- **ASfP Installed:** Verify you have ASfP installed and running.
- **Platform checkout:** You need a working AOSP platform checkout on your machine.

## Enabling Rust support

Here's the step-by-step process to get Rust support working in your ASfP
project:

1. **Enable Rust**

   - When creating a **new project** in the setup wizard, simply check the box for Rust:

   ![Rust checkbox in new project setup wizard.](https://developer.android.com/static/studio/platform/images/rust-new_project.png) Rust checkbox in new project setup wizard.
   - For existing projects, navigate to your `.asfp-project` config and add `rust` to `other_languages`:

           other_languages:
             -   cpp
             -   rust
           ```

       <figure>
             <img src="/studio/platform/images/1-modify_asfp_project_languages.png" alt="Modifying the .asfp-project file to add rust to other_languages" class="screenshot">
             <figcaption>Adding Rust support in the <code>.asfp-project</code> file.</figcaption>
           </figure>

<!-- -->

1. **Sync your project:**

   - After performing the previous steps, you need to trigger a project sync to ensure that all changes are applied and the IDE recognizes the Rust project: Go to **File \> Sync Project**.

## Verifying success

After completing these steps:

- You should see `rust-project.json` in your project view (under the source
  iml). This file is generated by Soong during analysis and symlinked to the
  repo root, which is necessary for LSP to provide language services. **DO
  NOT** edit this file directly; instead, re-sync to let Soong handle it.

  As a reminder, double check that `rust` is present under `other_languages`
  in your `.asfp-project` config.
  ![Ensure that rust-project.json has been generated.](https://developer.android.com/static/studio/platform/images/rust-verify.png) Ensure that `rust-project.json` has been generated.
- You should see Rust-specific features (described and shown below) enabled in
  your IDE.

## Key Rust IDE features

- **Code completion:** Get intelligent suggestions for Rust code, including
  items from other modules.

  ![Rust cross-module code completion example](https://developer.android.com/static/studio/platform/images/asfp-rust-cross_module_completions.png) Code completion suggesting items from a different Rust module.

  <br />

- **Navigation:** Quickly jump to definitions and find usages of Rust symbols
  across your project.

  ![Rust code navigation example](https://developer.android.com/static/studio/platform/images/rust-code_navigation.png) Navigating to the definition of a Rust symbol. ![Rust find usages example](https://developer.android.com/static/studio/platform/images/rust-find_usages.png) Finding all usages of a Rust symbol.

  <br />

- **Tooltips:** Hover over symbols to see type information and documentation.

  ![Rust tooltip example](https://developer.android.com/static/studio/platform/images/rust-cross_module_tooltips.png) Tooltip showing type information for a Rust symbol from another module.

  <br />

- **Inlay hints:** View type information and other contextual hints directly
  in the editor.

  ![Rust inlay hints example](https://developer.android.com/static/studio/platform/images/rust-inlay_hints.png) Inlay hints showing type information within the code.

  <br />

- **Structure view:** Navigate the structure of your Rust files using the
  **Structure** tool window (View \> Tool Windows \> Structure).

  ![Rust structure view example](https://developer.android.com/static/studio/platform/images/rust-structural_views.png) The structure tool window showing the outline of a Rust file.

  <br />

- **Refactoring:** Safely rename symbols and have all usages updated.

  ![Rust rename symbol refactoring example](https://developer.android.com/static/studio/platform/images/rust-rename_symbol_refactor.png) Renaming a Rust symbol and updating all its references.

  <br />

- **Real-time analysis:** Get instant feedback on errors and warnings in your
  Rust code.

  ![Rust real-time code analysis example](https://developer.android.com/static/studio/platform/images/rust-real_time_code_analysis.png) IDE showing real-time errors and warnings in Rust code.

  <br />