Getting started with Rust
This tutorial gets you up to speed with Rust development in JetBrains Fleet. It covers the installation, project setup, and working with code.
Prerequisites
JetBrains Toolbox 1.22.10970 or later: the Download page.
Rust: Install Rust.
Set up a workspace
Workspace is the directory where your project resides. It contains the project files and settings. You can open an existing project or start a new project by opening an empty directory.
In this tutorial we will walk through project setup from scratch.
Open a workspace
Press ⌘ O or select File | Open from the menu.
In the file browser, navigate to an empty folder where you want to store your code and click Open.
When you open a directory, it becomes the root of a workspace. You can view its contents in the Files view.
To start coding, create a project by using Cargo. Cargo is the build system and package manager for the Rust programming language. It provides a way to manage Rust projects, including building, testing, and managing dependencies.
Create project files
With your current workspace opened, click
.Run the following command in the command line:
cargo new find_averageNavigate to the main.rs file in find_average | src.
Open main.rs and replace code in the file with the following code:
use std::env; fn main() { println!("Average finder v.0.1"); let args: Vec<String> = env::args().skip(1).collect(); let avg = find_average(&args); println!("The average is {}", avg); } fn find_average(args: &[String]) -> f64 { let mut result = 0.0; for s in args { result += to_float(s); } result } fn to_float(s: &str) -> f64 { s.parse().unwrap() }
Smart mode
You can use JetBrains Fleet as a smart text editor, rather than a full-fledged code editor. However, if you need code intelligence features, you can enable them by turning the Smart Mode on.
Enable smart mode
In the top-right corner of the window, click Smart Mode, then Enable.
After you click the Enable button, you may have to wait for some time, while the backend is being prepared.
Running your code
With Smart Mode enabled, you can run your project. For that, you can use a gutter icon in the editor, or you can create a run configuration that will allow you to fine-tune the way your application should be run.
Run from the editor
Navigate to the entry point of your application and click the run icon in the gutter. Select Run `run <find_average>`.
Another way to run a program is to use a run configuration. It allows you to customize the startup: add command line arguments, use custom commands, and so on.
For example, in the following example we use executableArgs
to pass command-line parameters to our program. For more information about run configuration parameters, refer to Rust run configurations.
Create a run configuration
Click the Run icon (⌘ R) and select Create Run Configurations in run.json.
In the run.json file that opens, define running or debugging parameters. If the file is empty, press ⌥ ⏎ or click the file template link.
Alternatively, paste and edit the following code:
{ "configurations": [ { "type": "cargo", "name": "find_average_debug", "cargoArgs": ["run"], "executableArgs": ["1","2","3"], }, ] }Modify the configuration properties according to your environment.
Launch a run configuration
Click the Run icon (⌘ R) and select the configuration.
Hover over the created run configuration and click Run.
Debugging your code
Broadly, debugging is the process of detecting and correcting errors in a program. You can run the debugging from the gutter of the editor or by using run.json. For a tutorial on debugging, refer to Debug Rust code.
Each debugging process starts with setting a breakpoint.
Set a breakpoint
Click the gutter next to the line where you want to create a breakpoint.
Now we can proceed with debugging. As it was said, you can use the gutter icon or a run configuration.
Starting a debug session from the gutter
Click the Run icon on the gutter.
Select Debug `run find_average`.
You can use run.json to configure the debugging process, the same approach we used in running your code.
Create a run configuration
Click the Run icon (⌘ R) and select Create Run Configurations in run.json.
In the run.json file that opens, define running or debugging parameters. If the file is empty, press ⌥ ⏎ or click the file template link.
Alternatively, paste and edit the following code:
{ "configurations": [ { "type": "cargo", "name": "find_average_debug", "cargoArgs": ["run"], "executableArgs": ["1","2","3"], }, ] }Modify the configuration properties according to your environment.
Launch a run configuration
Click the Run icon (⌘ R) and select the configuration.
Hover over the created run configuration and click Debug.