25 Minutes of Rust (Episode 3)

In this third episode I explore the possibilities of the Rust (rls) Visual Studio Code extension.

I'm doing a series of 25 minute sessions where I try to get familiar with the Rust programming language. The blogposts in these series are the notes I took of the lessons learned along the way.

Warning to the reader: As always in this series, these are notes I take as I'm learning. They reflect my current understanding and may be incorrect!

Where was I?

Last time, I ended up being able to compile and run the Rust hello world app using cargo run.

>cargo run
   Compiling hello_cargo v0.1.0 (file:///C:/Users/username/Documents/Programming/Training/Rust/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 1.4 secs
     Running `target\debug\hello_cargo.exe`
Hello, world!

Now I want to dive into the features of the Visual Studio Code Rust integration.

Capabilities of Rust (rls)

The extension I'm using for Visual Studio Code is called "Rust (rls)". The documentation mentions the following features:

code completion

jump to definition, peek definition, find all references, symbol search

types and documentation on hover

code formatting

refactoring (rename, deglob)

error squiggles and apply suggestions from errors

snippets

build tasks

Before I start exploring Rust itself, I'd like to get familiar with these functions.

Code Completion

In my hello world app, I added a function called say_hello:

fn main() {
    say_hello();
}

fn say_hello() {
    println!("Hello, world!");
}

Typing say_ I would expect the plugin to complete my code:

Typing "tab" in the situation picture above completes the rest of the code, just like Visual Studio would do. Since I'm personally very comfortable in Visual Studio, this seems to make sense.

If I add multiple functions starting with say_ and type that, I can choose with the up / down arrows and then tab to use the selected piece of code.

Jump to Definition

I don't have much code yet. But I could jump to the function I made. I'm going to assume it works across files.

I like "peek definition" too. It shows you the definition of the function you want to look at, but your editor and cursor stay where they are. So you're not jumping back and forth through various files.

Since I haven't learned how to split up my Rust into multiple files, I can't try these two features yet.

Find all references works by showing a list of the lines of code where a given symbol was used:

Types and Documentation on Hover

If I hover my mouse over the say_hello function, it shows me the types of the function. Not so interesting here since the function has no input or output. I'll revisit this later when I have some more code.

I did notice it doesn't work when hovering over a function like println.

Code Formatting

I expect this feature to correct incorrect indentation and things like that.

So let's try it.

I'll type my function like this:

fn say_hello() {
println!("Hello, world!");
}

I'll retype the semicolon ; and see what happens.

Nothing. Unlike what I'm used to in Visual Studio.

But, there is a shortcut: Alt + Shift + F. This correctly indents all the code in the file.

Spaces and Tabs

An indentation from the formatter is four spaces.

But that could be my Visual Studio Code settings because if I type a tab myself they're also spaces.

Refactoring

I really like that feature. Let's try a rename.

I put my cursor on the say_hello method and click "F2". I get a small textbox and can type the new name for the function. It correctly changes the name and the occurences of the function.

I hope it works well with multiple files though! I'll revisit this when I have some more files.

Error squiggles

Those familiar with Visual Studio, or a spell-checker know these well. If something's wrong, you get "squiggly" red lines.

If I hover over these lines, I get a suggestion of what the problem is:

Build Tasks

This is also listed in the features. As a last thing for this session, I really want to compile and run from within the editor.

In the "Tasks" menu of Visual Studio Code I get options to do Ctrl + Shift + B which gives me the choice to run cargo build.

Also in the Tasks menu is the option "Run Tasks", this gives me a little bit more choice:

Conclusion

I think Visual Studio Code with the "Rust (rls)" extension is already quite OK. I'd like to look into the Visual Studio support for Rust, but that might be for later.

Now that I have an environment I'm somewhat comfortable with, I am eager to start working through some actual Rust.

I'm mostly comfortable with this setup because I'm very used to Visual Studio. I can imagine other people have different expectations of their editors.

The only thing I'm missing is step-through Debugging. Maybe I can look into that in a later session.