Rust Analyzer hover, autocomplete, and error diagnostics in VS Code

7:05 Free RustSoftware Engineering Watch on YouTube

Rust Analyzer provides hover documentation, autocompletion, and real-time error diagnostics in VS Code. It also validates dependency versions in Cargo.toml, showing unresolved imports when versions mismatch.

Transcript

the next thing that we want to do now that the rust analyzer is installed is how would I use it what are some of the benefits so without getting into the details yet of what this actual piece of code is doing let's go and start a plane around so we have some of these statements might look weird if you've never seen rust before which is fine but let's just assume that I have no idea what these things are so I'm gonna hover and I'm getting a little bit of information it sees that there's nine implementations of these so I'm getting some extra information it seems that this represents the color preferences program output that sounds pretty useful if I'm not entirely sure so that's definitely like if I click here on the implementations I'll get this up anywhere it tells me where these things are coming from all these are meant to give you extra context and it is useful to have there so I'm gonna close this out it opened up this other file you can see here there's a new tab I'm gonna close it out because I don't want to go through those details so that is pretty neat another thing that will start happening is that I can get more context when I'm trying to find where things are coming from so let's just say for example if I want to do something with clap let's just reuse some of the examples here again don't worry about the details on how this is working or what does the use keyword do exactly but we let's just figure out how the auto-completion works so you'll see here there's a lot of different things to choose from and you will get the documentation right right there when you double click that double colon you will get the examples you can get a pretty good documentation over here that can give you extra context on what the things are that you want to use so in this case if I want to use arc the abstract representation of a command line argument so clap behind the scenes not behind the scenes but it's actually a library so it's something that we're gonna use for building a command line tool that you can actually use it's a extension it's a it's a package rather than extension that will allow us to help us out to build command line tools the specifics are not important but you can see here it this is already pretty cool because I'm getting a lot of information here there that is that is pretty pretty useful so this is all possible because of the rust analyzer and it is pretty pretty good so beyond that like if I click like say for example that one like cool do that and that's that's fine that's that's pretty cool but the another thing that is very useful is to see what are the errors that I'm gonna come up with so say for example if I have if I have a typo here this is help how about I call it like uppercase help and then I save that it will immediately tell me it's like this is probably not very good and I'm getting or diagnostic information so there is a method with a similar name or help and this is coming from you can see rust see so behind the scenes rust analyzer is calling rusty and all these other tools to provide me context on what is it that I'm doing wrong so pretty good help error here because I know I have a typo so let me let me do that and save it and as soon as I save it the red underline goes away this is also pretty useful if I want to do something here that is say for example unknown and I'm gonna say true and I'm gonna save that and I'm going to start getting multiple red lines one this one here and another one here because things are going to get out of hand so the the compiler and we'll see what the compiler is doing later but the compiler is complaining but I don't need to go and switch context and run the compiler run some other external tool to get this information in the editor this is what I would expect from a very good extension in this case rust analyzer shines because we are not necessarily need to go back to the terminal or back to the command prompt and do do some some command interaction and parse that output now if we were so inclined to take a look at the output we could click here for a compiler message and this is how and we'll take a look at compiler messages and error later but this is what the output would look like so this is pretty good and again we'll don't worry too much about the details on what things are showing up here because we'll learn what those mean and how to interact with those error messages later it's just enough to know that the rust analyzer is very good here because it allows us to try to get these things in working order so I'm gonna save these and get to to to a now a working spot another thing that I want to show you is with the dependencies if I go to cargo that tomo again we haven't looked into these yet this is the spot where you can put dependencies so there's a clap is that the name of the library this is the version so let me change to something that is very old like say for example two points two point zero I'm gonna save these and as soon as I save you will have to take a look here at the bottom and right here you will see some output so I'm gonna clip save and fetching metadata and stuff is happening right rust analyzer is happening indexing things are happening and all of a sudden I am getting red all over we have main here with red that my tap is now red so let's take a look and we're getting all kinds of different problems so what are those problems I hover again and I get an unresolved import like that that's not able to get import and why is that because I change the versions and suddenly none of these things exist because I was using an older version of the library so if I go quickly and make that update to 4.2 as before everything goes back back to working order you can see here that the pennies are pulling down and everything builds and everything's back again so definitely the rust analyzer has tons of features and I just show you two or three things that are very high level that are very useful and it will definitely help you in your learning journey of rust now again don't worry so much about the code just yet we'll just go bit by bit and in this case we are now using rust analyzer extension for VS code that will help us out