Rust Setup For Neovim (ft BashBunni) #bash2basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up YouTube it's me TJ DeVries and we've got bash here today with us for another episode of bash of Basics we're trying out a new format today where we do a little bit more pre-recording we're going to try and get straight to the content get really deep and Technical about the things you're interested in as you've been requesting and today we're going to learn a little bit more about rust but before we do that bash why don't you tell us a little bit about yourself hey guys I am fast bunny I am mainly a go developer and a technical content creator that's my focus and I first heard about rust through the memes on Tech Twitter so TJ's gonna give me some reasons why I should use rust instead of just via the memes so for today's video we're primarily going to be working on how to get rust set up inside of neovim some common workflows and if you want to know more things like an overview of the language or building your first project or things like that let us know in the comments smash the like button give us those algo signals to know you want more of this this kind of content all right so let's go ahead and get rust set up for neovim yeah TJ I gotta admit I did not set up my neovim config to do some brass Dev so let's do it we'll start from scratch let's go bash to Basics the first thing that we need to do is to install rust surprise the main way that you can do that is through rustup.rs on that site there will be a script that you can run to install locally on your machine to get the primary features that you need to be able to run rust those tools include cargo and rust C which is what cargo use under the hood to compile and execute rust I got rust installed now full on Raw station let's go after installation you should make sure that you follow the additional instructions to make sure that cargo's environment is set up which is usually printed out at the end of the script things like sourcinghome.cargo.m you might want to add that to your bashrc or zshrc if I know what you use to make sure that this gets sourced every time you run a new show so TJ all right I installed the rest of my machine I don't quite know how to install it like with my neovim setup so I'm currently using LSP installer like the deprecated version of mason.ums Nice um to manage my LSP servers so if I was more organized I would have Mason actually properly set up but it's my understanding that I'll probably need the Ross language server rust analyzer and starlark rust maybe for a linter so actually all you're going to need is rust analyzer rust analyzer actually ships with the LSP so before we can do that though we probably should make a new project to host some new things for us so I think yeah amazing the way that you make a new project and Russ is actually really nice and easy you just type cargo and then new and then the name of the project you'd like to do nice that's an excellent project name I like it wow it's a git repo and everything for you yes so it's all set to go it's all ready and it already creates an example main RS file with the sort of skeleton of what you're going to need uh to open and run this file so okay not a whole lot going on yet but it has everything that you need to be able to do that nice excellent guess bash we didn't even talk about that yet but I guess you're ready to go with cargo run I love it I was like I'm pretty sure how you can use cargo for this nice and you actually don't have to run it from inside the source directory in general inside of rust projects we want to operate from the root of the project oh it's interesting so if you go here and you just like cargo run again cargo can still figure out what you want to do it doesn't need to be right in that in that folder awesome so here it seems like uh I've got the little rust logo down here but um I don't know if I have my LSP there's nothing attached right so now that in the stats right now so if you just started like typing we're not going to get errors or anything like that it's just going to look like nothing going on and this is sort of like if you were just writing rust you would be feeling like this is a bad time because you're like I don't know what I'm supposed um definitely for rust one of the things that you'll want to do as you get more experienced with it is you learn that you really want to rely on the tooling inside of us because it's really made to be able to help you with these kinds of problems so exactly like this printf knife onto scope how do we get that error to show up in the editor and that's what we'll do right now perfect so the first thing that we need to do is we don't actually need to install this with some other tool rust actually can manage its LSP within itself and that actually works quite nice because that's going to mean that the same version of rust that you have is going to be using the same version of that LSP which is good for things that might have you know sort of like breakages between rust versions um so that's so that can be nice for certain like weird macro edge cases that you definitely won't run into at start but sometimes happen later right okay okay yeah because I'm used to using my like LSP install to to just like install the language server but I think you mentioned that yeah like I don't need to do that and I can just tweak something maybe in my DOT files yeah so we can we can just do that straight from using rust up again and maybe bash you want to remind people what actually is an LSP right like what what is it uh it's the language server protocol and it basically allows you to get that that functionality where it is going to do some like suggestions for you errors things that are related to basically like the language syntax and things like that that you'd expect from an editor your LSP is what's going to do that for you in something that's a little more um granular like neovim yeah and so the nice thing about it is it's just a separate executable right it doesn't have to be like magically something that any of them has to have a lot of information about what we can actually do is we can actually tell rust up hey rust up we'd like to add a component so we say component oh do I do rest up or no yeah a rest up component and then add yeah and then rust Dash analyzer and so this will tell rust up hey I would like you to make sure that this version of rust has rust analyzer so I'll say I can do that downloads and installs okay so how do I so now if I were to like go back um do I have do I have to like quit any of them and reopen it for the LSP to attach well so so that's the first part right we've now installed rust analyzer it's available to be run on your system but now we have to tell me of him I would like to use rust analyzer so now we go back to your LSP configuration okay and this is where we're going to need to tell LSP config.rust analyzer and we're going to call setup now LSP config already is aware of information about rust analyzer right like how to call it and different ways you can find it you'll need an underscore actually for this one though I think I should uh attach a Lua can I can I quickly attach a Lua language yeah so this is the other other ways that you could do LSP install and then uh for for people on Mason it will be Mason install but it's basically the same idea and then that installs some Mako so that's right Nora that I have outdated LSP servers yep it's also possible for us to do this with rust analyzer but I just wanted to show that it's not a requirement to use these other package managers to do this and then would I do something similar so here I have like capabilities equal to capabilities on attached is on attached all of that like that seems like pretty generic exactly right so that just tells LSP config hey when you attach to this LSP I want you to run my on attached function and it also says advertise to this LSP that I have these capabilities but we don't need to pass settings or Flags in this case oh okay yep so we can just uh delete those if we're not going to set anything to them they'll default to good values okay cool so now ideally when we open up uh we'll have to restart your other neovim uh and like open back up but hopefully what happens once we uh run once we open this again is that we'll start getting uh some errors oh goodness spawning research server with command rest analyzer failed yep so this this is fine this just says LSP config doesn't know how to find the particular rust analyzer that we have and this is actually pretty easy to solve as well so we can go back to your config so we can override what the default command is for rust analyzer by typing uh adding a new field here uh command CMD and then equals and then we're going to make a list of uh here and inside of here we're going to say rust up as strings and then stable this tells rust up which version of rust you want to use so when we installed the default is stable so we want to use that one and then the last argument is rust Dash analyzer so this just says Hey rest up I installed rust analyzer with you please now execute rust analyzer uh for me okay and so now hopefully when we go back so this this command so obviously I have you like navigating me through this but if I wanted to see like what if I wanted to like do use help or something like that to see what um the configuration should be for rust analyzer like how would I do that yeah the main place you should go check for some of those things would be in LSP configs repo there's a great list of all the different configurations and they might even have that in the help docs as well I don't remember offhand but they'll tell you some of the information and then the the other thing is to sort of generally know that like if you look for rust analyzer on their website they'll tell you how to execute rust analyzer if you've installed it in these things okay gotcha so we'll link that in the description then as well just for those who are interested because I think I got that question a lot and there are definitely times where I struggle with that as well yeah I think the main the main thing is like a bunch of these sort of overall initial settings for LSP config will really help you quite a bit to understand what's going on and then this gives you an example of like oh I want to run a different command to execute this language server this can be really useful to like decide lots of different things like if you're going to run a particular version of a language server for particular reasons right you might set up your thing to to do that uh depending on what the circumstances are yeah that makes sense and it looks like the help is like mostly it tells you like what the options are but it doesn't give you like obviously examples for each of the language servers so but that is in the server configurations uh documentation on the LSP config repo so you can go check that out for like particular sort of oddities about each Lang and they link to wherever the documentation is for those language servers as well okay so now that we've told LSP config hey here's how you actually run that executable right because lsbs are just cyberband owners now LSP config says oh okay cool now I know how to run it so when we open up our source main file again after rest analyzer has a short amount of time to start actually running it's going to say hey printf doesn't exist and now bash you're back to all of your normal ways that you're able to execute and do different things in um in the oven oh yeah that looks oh there is no printf there's no there is no print theft that's your go Lane Bry go Lang brain getting messing with you because it actually tells me what functions to use there you go but you can actually use the printf style things that you want to do in print line maybe that's the very first thing that we can show so if you type print line here instead of print which is usually what we use and then inside of uh this first string you can oh close close that's a good guess but it's actually curly braces it's more like python F strings than uh then print that exactly so it's like that so uh no so just the curly braces and then you could say like hello or something outside of the curly braces right so that there's like some string this is like your format string yep nice there you go so now do a comma and now yeah exactly wherever I want it to be so what's happening here is that print line is actually a macro in Russ anytime something ends with an exclamation point that means that it's a macro so it's going to do additional magic sort of that runs these things and it'll make that work so you can put as many arguments as you want in there you can add additional specifiers you can do all sorts of different things and it will continue to keep working and so that's just exactly what's going on there and if you put too many things inside of here nice I love it if you put too many it's going to say yo that doesn't work you don't have enough things inside of here to fill right so you need to add another pair of curly braces and because my it looks like because my LSP setup is pretty generic like all my keybinds and everything like that like as soon as that language server has attached to any of them like it I can still I can use all my keybinds that I'm used to like the Diagnostics list and stuff exactly right that's one of them into it it's one of the nice things about using neovim for this after you get your LSP setup going is you want to add a new language to your repertoire you want to explore all you got to do is get the LSP going and then it feels like exactly that same experience as you've had rather languages yeah that's great all right so I just added a new function called truthy and a test to test our truthy function I also amended our print line to also call our truthy function but uh TJ the problem is is like as Advan Advanced of a rust developers this makes me I actually don't know how to run the tests so well of course the main thing about it is not actually ever running your rust code it's about just like writing some so you can tell the people on Twitter about it but since we're here already like I may as well tell you how to run the tests so if you had to guess though bass we actually haven't discussed this yet what do you think the way that you could run a test would be a cargo test yeah well let's just see so sure enough it finds and discovers those tests and like all good tdd developers right we wrote the failing test case first so that we could show you that that test will fail so if we unsuspend any of them and go back and we change false to true and we run the tests again oh false the true yes yeah probably we want truthy to return true otherwise it's fake news there's the other there's the other solutions to failing tests which is just make the tests fast my initial reaction is just so yeah let's change the test there's something wrong with it we're failing I like that strategy too I like that strategy too all right cool test pass and you'll now see that they pass but what I want to show you is just some of the ideas to get you started on really improving the iteration speed of how fast this can be run and how quickly the test can be run so let's hop back into neovim okay let's undo the last chain just so that we can see it being failing again and I I tend to run these in neovim but you could use tmox or any of the other things that you wanted to do so let's open up a terminal in a split here can I how do I how how do I actually do that do I do VSP terminal um you can just do VSP and then do terminal oh right yeah because then I can just gotcha and so now this is the terminal just like normal right but we're gonna add a new tool so we're gonna first do cargo install cargo Dash watch there's many different packages in the sort of rust ecosystem that improve your cargo experience and a lot of them uh start with cargo to tell tell you this particular package basically watches all of the rust files that are in your repository and whenever you save one it will run a command again for you uh type out cargo watch Dash X and then test yep just like that and so now you'll see that the test failed after we were running that so now if we navigate back to where we were editing our code oh uh TJ I actually don't know how to escape the terminal this is a problem that I've had before control slash control control n but you can map that as well if you want control n uh control backslash sorry both of them are control all right nice okay so yep after navigating back and now saving that file you see immediately it runs again and now you see that you get green that's so cool yeah and so you can keep on doing this over and over you can save and it'll run again and fail if you introduce some syntax error by right we're trying to return something that doesn't make sense like returning five then it will also tell you that it failed to compile right and so you can sort of just keep on working through what you're doing here and it'll say oh you know you failed to do this blah blah yeah this is one of my favorite ways to work through different problems in Rust it's really iterative it feels really fast and quick and it's just recompiling what it needs to do and giving you that sort of like inst almost instant feedback about what you're working on and then you just keep on going I write a new test do whatever you need to do Etc and it'll just automatically discover find those and run them for you that's awesome that's so nice I love a language with good built-ins that's what I love about go is like some of the build the built-in functionality like they're built in test suite and stuff like that so it's really cool seeing that rust also has like some pretty amazing built-in stuff yeah definitely agreed so already just getting my new of him set up and getting this basic main.rs uh function running it's got me really excited about learning rust and even how simple it actually is I feel like I was very intimidated by the prospect of learning rust but it doesn't actually seem so bad especially when you actually have an LSP that's working in neovim and it tells you what to write that's great so TJ I hope that we're going to be able to learn more rust at I.E you're going to teach me some Rust um but I think that we got to make the people work for it a little bit so you can get it for free no free lunches they gotta like the video uh if we get a thousand likes on this video we will do a follow-up to this where TJ is going to teach me rust as a complete noob and don't forget to subscribe to stay up to date on when that new episode launches yeah in that next video what I really want to shoot for and aim for is basically the Bare Essentials of what you need to know to start writing rust on your own obviously we can't cover every language feature but what I hope to do in approximately 15 minutes is give you the general overview of how do I start writing things and how do I start writing things to run into those problems of the advanced features of rust and maybe you'll need to struggle with maybe you'll need to learn some more things or maybe we'll cover in subsequent follow-up videos depending on how things are going depending on what the algorithm is telling us to do so that's sort of the plan that's what we're looking for to do let us know if you like this new style do you like seeing us do this a little bit more cut up a little bit more edited do you miss the streams let us know in the comments we read them all we like them all you'll see a lot of Hearts from me I love to give hearts out this has been bash to Basics I'm TJ I'm bash and we're super happy to be here learning with you and just enjoying this time on the internet together thanks everybody we'll see you later with your rust friends [Laughter]
Info
Channel: TJ DeVries
Views: 73,880
Rating: undefined out of 5
Keywords:
Id: Mccy6wuq3JE
Channel Id: undefined
Length: 20min 2sec (1202 seconds)
Published: Fri Jan 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.