Transcript
This transcript was autogenerated. To make changes, submit a PR.
And welcome to Learning Rust by building CLI tools.
Some intro about me could find me at
Jim lynch codes, also in the New Jersey New
York City area. Like to do lots of things.
Been working as a software engineer for about ten years.
I wouldn't consider myself a rust expert.
Been coding in it for a few years, but sometimes
still feel like I'm kind of a noob. And I would say my
progression of my favorite programming languages started
back with action Script, three flash games,
and using Java for the back end and VBA for
Microsoft applications. Then it kind of progressed to
typescript, react and node js
and all those kind of things. Got into clojure for a
while, very functional lisp syntax,
and now I feel like Rust is my favorite
programming language. Kind of steals all these good things
from the other ones. So why do people come
to Rust? Well, it's probably faster
than what you were using before, and it's also safer than C,
C Plus plus, and less painful, dangerous to code in.
There's no garbage collector virtual machine,
and here is kind of a famous picture.
This is from the blog posts of why discord
is switching from go to rust. And you can see these spikes
are basically the cpu working really hard,
the response times taking a lot longer
than they should. And this is pretty much from the garbage collector
kicking in. Okay, so once people start using Rust,
why do they end up falling in love with it? Usually the
super elegant syntax, right? Just the fact
that variables can never have a null value. You have
this explicit syntax for all meetability
fields, visibility heap for stack allocation,
and it has a very functional ish feeling syntax,
right? Just the match syntax and how if is
an expression. Here's a chart that Rust developers
love to show. This is the GitHub most loved programming
languages, and we can see rust at
the very top, number one by pretty large margin.
Okay, so why use Rust
for CLI tools? And why CLI tools as
a way to learn Rust? Well, CLI tool
is the default thing that's created when you run cargo new
and create these new project. These CLi tools are
usually small, isolated projects, which makes it
easy to just practice one specific thing.
Rust. And the speed and efficiency of
rust make it a great choice for building a
real CLI tool. One day you could
use go or python or node js or something
else, but even just that little bit of
garbage collector virtual machine that has to boot up in the beginning,
you'll usually find the split second of delay,
and that can be annoying for some people.
So it can be nice to build them in
rust and then you just have everything execute extremely fast.
But how do you know that it works? So you
can use these small exercises as a way to practice not
just writing the rust code to complete the task,
but also to practice writing automated tests
to make sure your rust code works. Okay,
key thing is to put your unit tests and integration tests
where cargo is expecting them. And here's
a little example. Say we're testing this lib rs in
the source folder. You'd put your implementation
code for lib rs in that file, and you'd
also put your unit tests for lib rs in that same
file. Then in these
integrationtest rs,
you would put your integration tests and basically all the
files inside of that tests folder that's parallel
to source would hold all integration tests.
Okay, you can also use these exercises
as a way to get familiar with the macros
used for testing, like these CFG test and assert
books. Books are nice too. Here are a few
books. There's lots of good books on rust, and I'd
recommend checking them out. Reading them
before, during after you're working through these
exercises. The GitHub repo remember my handle
is Jim lunchcodes. So they're at GitHub.com
jimlenchcodes rust CLI exercises.
And I also put together some example solutions.
So these are my own personal solutions, not necessarily the
only ones or the best ones, but you can find these@GitHub.com
jimlinchcode rustclixamples and
I just wanted to go over this real quick for people who are totally new
to rust. Really awesome getting started with
rust because we have rust up and you can
find it at Rustsup Rs. Just one command works
on Mac, windows and Linux, and it will install cargo,
the compiler, everything you need for developing
in rust. Okay, so with that out of the
way, let's jump in and explore the exercises.
Okay, so before we take a look at the code, just wanted to show you
how to work on these projects.
Okay, so we're here at the repository
and I recommend making a fork of
this. You can't push directly to my
repository since you don't have permissions.
I'm just going to choose one of my organizations
here as these owner and you can copy the
main branch only. That's fine. And the idea is that
when you create a fork, this will copy all the files
into a repo that you own. So then
you can clone this, work through the projects,
and push your solutions directly
up to your repository here. Okay, here we are in vs
code and I'm going to go
through some of these files here. First, let's look at the readme
in the root of this project. Okay?
And this is saying again, we can use rest up to install
cargo and everything. We can run cargo version to
check that we have installed. And these, in each of these
projects we can run the commands cargo
run and cargo test.
You can also run these tests with some code
coverage output using this tarpaulin.
And if you want to get crazy, you can even run
mutation testing. Okay?
And you can work through these in any order. They're totally independent.
So the anatomy of an exercises. Each exercise
will have a title, will have kind of a little backstory,
an imaginary real world situation of where
this could be useful. The exercise, the actual
goal thing you need to accomplish some suggestions
about how to write tests for this bullet
points of the skills practice some things
that you can add to your rust toolbox of just
things that you're confident that you can do in rust.
Bonus sometimes there will be bonus ideas
for how you can push it further.
And in a hints MD file,
I've put some hints in case you get stuck. And just the
idea of building your own rust toolbox where you
have all these little examples of code that you've written.
So if you need to do that again in the future,
you have it right there in your toolbox as a reference
or as code that you can just copy right over and
allows you to just really hit the ground running.
Okay, we talked about this a little bit. Why rust
is great for CLI tools. Fast execution
and startup time. Very low level control
with these nice syntax. Cargo is
a really awesome build tool and
CLI tools just for working with rust.
And coding with rust is know once you get the hang of it,
you might find that you actually really like working in rust.
And you really don't want to go back to coding in typescript
or python or whatever you were doing before.
Have the solutions these in these rust CLI examples.
Repo contributing I would
love to have some other faces on the repo of
people who have contributed, maybe fixing
typos or even adding more exercises
here. Totally open to that. So open some
issues if you have ideas or pull requests,
if you want to just jump into working on it.
And this is all MIT open source, so feel free
to use this code for whatever personal
or commercial projects that you have. Okay, now I'm just
going to briefly talk about each of these projects.
Hello world, the classic
example. All you need to do is write a function that prints hello
world bin files helps
you to understand breaking up your functions into
different files and modules and rust argparse
since we're building a command line tool,
you probably want to parse arguments. So that's
positional arguments or flags
that you can pass in either the long
flag with two dashes or these short flag with one dash and
these potentially taking arguments with your
flags. For env reader, this is
reading environment variables. If you have API keys
or secrets, definitely recommended to
save them into an environment variables file
or somehow load them rather than just
hard coding these into your code and pushing them up to get number
five, table of chairs. This one,
kind of goofy. We have a bunch of data about chairs
and you want to render them in a table and
this just helps you practice rendering a table
of data in the command line. Number six,
Fibonacci. In this one classic
Fibonacci series, you're given a number and you want
to return the Fibonacci series
that ends with that. Number seven
apology letter. This has some
string parsing, some file loading
read write. Basically you have an apology letter with
some placeholders and you
pass in the person's name and
the thing you're apologizing for and we'll automatically generate
an apology letter. Number eight, image resizer.
This one you want to load an image and
save that image in different sizes.
Number nine GitHub scaffold. So in this one
you're writing a CLI tool that automatically
scaffolds a project from a repo on GitHub.
So it's automatically doing that clone and pulling the files.
Number ten, yes no. So in things one, you want to
take input from the user not
through arguments, but by
waiting. It's kind of that give and take question
sort of CLI tool where the
program will run. It will ask you hey,
we need an input, yes or no? And then the program will just
sit there and hang until the user enters
something, and then it will respond
depending on if the user enters. Yes or no.
Okay, number eleven, get json. This one
is basically just making an API call, making a get rest
call and parsing the jSon,
displaying it. Okay, number twelve, happy birthday.
This one has to do with working with dates. So basically
you're given a birthday of the user and
you need to display if it is the user's birthday or not,
and if not, how many days until their next birthday.
13. Agile stand up. So in this one
you're kind of just taking text input
from the user. That has to do with agile
stand up. So you're reading things like
what did the person do yesterday,
what did they work on, what are they working on today and if
they are stuck on anything. 14.
Pizza order so another one where you're taking text
from the user. This one is about what
type of pizza they would like, topping,
size, and those kind of things. 15.
Higher or lower? So this is sort of
a guessing game where the
program will think of a secret number and you have to guess
between one and 100. And as you give wrong answers,
it will tell you if the correct answer is higher or lower.
16. Wordle game so this one,
no relation to New York Times or Josh Wordle
or any of them, but this one is building a
five letter word guessing game.
Seventeen s these read, write. So this
is reading and writing to an Amazon
s three bucket. 18.
Database crud so this one you are
building. Create, read, update, and delete functions with the
database. You have lots of
freedom here to use any kind of database
you would like. And I think it would be really interesting
in my examples repo to have
lots of different examples here of
doing these crud operations with different databases.
19. Audio stutter so this
one you're given can audio file and you
need to make a cool stutter effect.
And 20. Traveling salesman where you're given
a graph of weighted edges and you have
to write a function that finds these most efficient path.
So that's all the exercises. Go through them.
Have fun. Let me know if you get stuck.
Thanks for watching my talk. Hope this was
helpful and happy coding.
Bye.