New user - rustlings & vscode

I decided to try rustlings as a first step to getting more familiar with rust. (my experience is mostly in Java/C/Python)

I ran the installer - already had the prereqs installed via homebrew on macos.

I then opened the root directory of the git clone in vscode. I installed an appropriate set of extensions in a clean profile, including rust-analyzer.

I've also run rustlings lsp (and reloaded the workspace) but I have only syntax highlighting in vscode - no warnings/errors. I do get hover help though for the functions.

The generated file was:

{"sysroot_src":"/Users/XXX/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library","crates":[{"root_module":"exercises/clippy/clippy1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/clippy/clippy2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/clippy/clippy3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/conversions/as_ref_mut.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/conversions/from_into.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/conversions/from_str.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/conversions/try_from_into.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/conversions/using_as.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/enums/enums1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/enums/enums2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/enums/enums3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/error_handling/errors1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/error_handling/errors2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/error_handling/errors3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/error_handling/errors4.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/error_handling/errors5.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/error_handling/errors6.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/functions/functions1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/functions/functions2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/functions/functions3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/functions/functions4.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/functions/functions5.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/generics/generics1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/generics/generics2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/hashmaps/hashmaps1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/hashmaps/hashmaps2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/hashmaps/hashmaps3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/if/if1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/if/if2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/if/if3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/intro/intro1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/intro/intro2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/iterators/iterators1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/iterators/iterators2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/iterators/iterators3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/iterators/iterators4.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/iterators/iterators5.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/lifetimes/lifetimes1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/lifetimes/lifetimes2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/lifetimes/lifetimes3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/macros/macros1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/macros/macros2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/macros/macros3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/macros/macros4.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/modules/modules1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/modules/modules2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/modules/modules3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/move_semantics/move_semantics1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/move_semantics/move_semantics2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/move_semantics/move_semantics3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/move_semantics/move_semantics4.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/move_semantics/move_semantics5.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/move_semantics/move_semantics6.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/options/options1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/options/options2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/options/options3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/primitive_types/primitive_types1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/primitive_types/primitive_types2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/primitive_types/primitive_types3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/primitive_types/primitive_types4.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/primitive_types/primitive_types5.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/primitive_types/primitive_types6.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/quiz1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/quiz2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/quiz3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/smart_pointers/arc1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/smart_pointers/box1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/smart_pointers/cow1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/smart_pointers/rc1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/strings/strings1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/strings/strings2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/strings/strings3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/strings/strings4.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/structs/structs1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/structs/structs2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/structs/structs3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/tests/tests1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/tests/tests2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/tests/tests3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/tests/tests4.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/threads/threads1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/threads/threads2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/threads/threads3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/traits/traits1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/traits/traits2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/traits/traits3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/traits/traits4.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/traits/traits5.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/variables/variables1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/variables/variables2.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/variables/variables3.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/variables/variables4.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/variables/variables5.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/variables/variables6.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/vecs/vecs1.rs","edition":"2021","deps":[],"cfg":["test"]},{"root_module":"exercises/vecs/vecs2.rs","edition":"2021","deps":[],"cfg":["test"]}]}

I also tried adding

{
    "rust-analyzer.linkedProjects": [
        "./Cargo.toml"
    ]
}

which felt unnecessary from the docs - though it made no difference.

Any tips on getting the extension to work

I should add that rustlings itself is working well - it's a nice approach. It's just that moving forward I'd use vscode, and that ide support is a big timesaver over just cli usage.

It appears as if errors are shown if I introduce them into quiz1.rs etc at the top level of the project, but not within the exercises (ie exercises/variables/variables2.rs) so I presume it's something related to the project setup for the plugin

I was able to get some syntax assist by adding this into cargo.toml (found from some google searches)

[lib]
name = "rustlingslib"
path = "exercises/mod.rs"

which I guess pulls the exercises into the project so RA finds it.

Even with this, though some errors, such as adding a random character in the file, show up as errors (squiggles & problems), others do not -- the E0283 for example in variables2.ts. I see no red squiggles in vscode, and nothing under PROBLEMS

I had a look at the rustlings repo and it looks like rust analyzer is not going to understand the exercises (but keep reading, there's hope!)

They aren't part of a rust project, so rust analyser doesn't "see" them. That explains why some errors don't get detected.

I suspect the errors that do get detected are for code in src/ folder of the git repo. This is the rustlings binary project code and rust analyser should understand it.

It appears that there is an lsp command. This writes a JSON file that rust analyser will use to "see" all the exercises. (I had a peek at the code in src/main.rs)

It is documented in the readme but, it's pretty far down. I'm not surprised you didn't find it :slight_smile:

rustlings lsp

That should sort everything.

Good luck and make another post if you have a question about an exercise.

1 Like

For the curious rust-project.json is discussed in the rust analyzer manual here.

It is needed when your rust code isn't managed by cargo. But cargo almost always will be used for a "real" project.

Thanks @drmason13 - However I did indeed find rustlings lsp and ran it (in the first post :wink: ). Unfortunately it didn't help. I'll need to take a read through a) the source code of what that does and b) learn more about cargo.

Specifically it will be interesting to see if this is 'just me' - something about my environment (I may try in a clean ide elsewhere for example) or common. Obviously if the latter I'll raise an issue/figure out PR..!

I think having the IDE work properly with rustlings is important.

Ah my bad, it seemed like a good solution at the time!

rustlings lsp should make most features like semantic highlighting, inlay hints for types, completion, autoimport and other code assists work, and also rust-analyzer's own diagnostics (error squiggles) which cover e.g. syntax errors and some simple type errors. It won't make rust-analyzer able to run the actual compiler against the exercises though, which means you still don't get the more complex compiler errors, you have to run the rustlings program in a terminal for that.

It might be good to explain that in the rustlings readme to set correct expectations :thinking:

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.