Rust Anyhow ? Turbofish ::<> / HTTP Calls and Errors @ Rust Meetup Linz (Austria)

JFrog is a proud Platinum Member of the Rust Foundation

January 28, 2023

2 min read

Anyhow ? Turbofish ::<> / HTTP Calls and Errors in Rust

Rust is a very powerful and full-featured systems language. However, its native idioms (at least in their default implementations) can be quite verbose. Even when you just want to GET a JSON document and do something with it. Explicitly matching every Result case is required to avoid panic. Or is it? Let’s learn together to use turbofish syntax and the anyhow crate to produce more elegant but still idiomatic Rust code that allows us to explicitly handle Result(s) as necessary. We’ll demonstrate making Rest API calls using Rust request and we’ll show how to incrementally minimize the amount of boilerplate it takes.

Elliott Frisch is a Senior Developer at JFrog with over 20 years of software design and development experience in a variety of languages. He has been working in Rust for the past year at JFrog on Pyrsia, an open source decentralized package network. His personal mantra for contributions to software is: producing exceptional products that delight users and stakeholder alike. He enjoys helping other developers with tough challenges and he is currently ranked in the top 500 users of all time on Stackoverflow. His other hobbies include a lifetime of reading and watching science fiction.

 

 

View Slides Here

Speakers

Elliot Frisch

Senior Developer, Pyrsia Team @JFrog

Elliott Frisch is a Senior Developer at JFrog with over 20 years of software design and development experience in a variety of languages. He has been working in Rust for the past year at JFrog on Pyrsia, an open source decentralized package network. His personal mantra for contributions to software is: producing exceptional products that delight users and stakeholder alike. He enjoys helping other developers with tough challenges and he is currently ranked in the top 500 users of all time on Stackoverflow. His other hobbies include a lifetime of reading and watching science fiction

Video Transcript

thank you
foreign
[Music]
foreign [Music]
hello and welcome hello and a warm welcome here to our rust Lintz Meetup
the first Meetup in the New Year 2023. it’s January and it’s not a
Thursday today that’s a little bit different compared to other rustling’s Meetup appointments today we had to
change a little bit because of a date conflict of Stefan Stefan who will be
with us in a few minutes he will do a talk today um so we decided to Simply switch our
rust lens Meetup this month to a Monday and we hope that is okay for you we are
super glad that you are here and we are super excited for having two awesome
speakers I will tell you a little bit about our program in a second let me
quickly share my screen with you um I need a little bit of a setup here
and start that one and share my screen here it is so we will kick off the day
with the evening with a round of community news this is our 29th Meetup
wow it has been a while and um yeah next Meetup will be our 30th Anniversary we
are really looking forward to it we are currently looking for um additional speakers we have something
planned but we still have one slot open so if you want to do a talk at the
roslings Meetup don’t forget to send us an email or just go to our website rosslins.at and there you’ll find the
sessionize link where you can always enter your talks if you want uh and with
ads let me tell you a little bit about our community news as you all know our Meetup is brought to you by the coding
Club lens the coding Club lens is um uh a community of passionate coders who
come together and they they do free meetups and coding clubs for kids and
things like that that is what the coding club links is all about and today’s speakers let’s go right into the topic
the first speaker is Elliot Elliot Frisch will start the will kick off the
day you know that rust Lintz is a beginner friendly Meetup so I think this will be a very interesting talk
especially for those of you who are not yet the perfect experts in Rust maybe
you wonder uh hey what is this turbo fish syntax this strange turbo fish syntax all about
um or what is this any how and how does it relate to result and things like that Elliot will give us a tour
um on on topics like that and we are really looking forward to to his talk
um Elliot’s talk will be somewhere between 20 minutes 25 minutes something like this and then our weary own Stefan
Baumgartner we’ll do a second Talk of the evening Stefan will do a great talk
about Builder pattern and type State programming so for his talk this will
will kind of be the next level here in our today’s Meetup so if you are
designing apis in Rust I think this will be a very interesting one we are looking
forward to it obviously so uh if you would like to join the discussion ask questions for our
speakers and so on you can always do so just join our Discord server here you see the link I already pasted the link
on the uh in the YouTube channel um and uh if you want you can join us
there and if you ask questions there I will take these questions and relay these questions to our speakers you can
also use the YouTube chat if you would like to ask a question feel free to use the YouTube chat and our speakers will
see you there and I will discuss it at the end of their talks with the speakers
now let’s dive right into the topic of community news as always we spend a
little bit of time to introduce you to what’s going on in the it Community around the coding club and in general
around rust and Lindsay and so on the first thing traditionally is um the the
note about our coder Dojo links for those of you who are new here at the Ross Lanes Meetup coded Dojo is one of
our most important areas of work here at the coding Club lint coded dojo’s exist
all around the world and we run Austria’s largest and most active code adojo here in linse it is a programming
Club a volunteer driven programming club for kids between 8 and 17 which start at
the very Basics with block programming with scratch for instance or Minecraft education Edition and things like that
and we work our way up through web development HTML typescript all this
stuff and even semi-professional software development for uh students young student who are who study in some
kind of technical schools our next meetings will be this Friday and this
Friday is something special we have two different meetings on the one hand side we meet personally at the topography
here in linth so if you would like to visit the coded Ojo personally if you live near links for instance come to us
that’s the first Workshop that we have and the second series of workshops will be as always online workshops so even if
you don’t live here in Lintz uh your kids niece nephews whoever they are they
are more than welcome to join us here at the code of Dojo lint we publish the program of Friday always on Wednesday so
take a look on the web to the website here lins.co.net on Wednesday and we will see the workshop program for Friday
everything is free everything is free of ads it’s completely volunteer driven so
we are looking forward to see you here by the way Dakota Dojo lens is in German so the children who join us there have
to be able to speak and read German the next one is our friends at female
coders female coders is a partner organization we love to work with them we have a long standard relationship
with the female coders programming Club here in lint and their next study groups
are in February I think it’s on the 8th of February and if I can remember correctly that will be a Vienna Edition
so I think the female code is Lintz will be guest at uh at a female called as
Vienna but please check out their website it’s female-coders a t and there you find all the details so if you are
if you are a girl or a woman who work in I.T in software development or if you
would like to get into software development and you are looking for like-minded women and girl girls who
always who are already in this in this Branch or who are also learning that
female codus is a great community next one is technology plascial
technology Plus show is I think the largest uh meetup group that we have
here in links after a kind of covet related break they are back
um now a regular technology Plus show are going to happen again and the next one is just in a few days it’s on the
26th of January and that is exactly the reason why we shifted the rust Lanes
meter to Monday in this week so if you are interested in um in in the
technology Plus show please check out their website it will be an in-person event so you can go to the technology
plowshow and meet all your peers and friends in person I would like to give a
a big shout out to Matthias who will do a presentation at this week’s technology
Plot show he will talk about from zero to production with rust you know Matthias he is a regular speaker here at
Ross Lynch he is a rust hero a local rust hero who we are proud to have here
he did two talks about rust here at rust Lintz and he will do a talk at the
technology browser of course also related to rust so this is a great meet up you should definitely not miss that
one and last but not least um we got um a promotion code from the rust Nation
UK this is not really relins and it’s not really online but still Stefan is in
in contact with the organizers of of rust nation and therefore he got a promotion code for minus 15 to
rustnationuk.com so if you want to go to this um to this event and you would like
to have 15 off of the price then feel free to use this um uh this this coupon
code here and that’s it that’s our community news
um that’s all I wanted to show you and with that let’s take a look is Elliot
already here yes we have Elliot already here let’s get Elliot our first guest speaker on to the stage hello Elliot
hello everyone here thank you so much for having me uh it’s very exciting to
have this opportunity to speak with the Community The Wider rust community
so I’m excited to share my talk today’s talk is titled anyhow uh turbo fish and
um we are going to that sounds that sounds
awesome thank you thank you Elliot let me also say thank you very much that you take the time to give us this this talk
um we we really try to be a beginner friendly uh rust Meetup so I think that we will be super interesting to have
somebody like you it’s it’s important for us to not always have these super deep dive stuff where only a few rust
experts can really benefit from it but that we cover a broad range of topics
and I know from experience I’m a trainer myself that things like turbo fish are not so easy to understand for people who
get right into the language so we are really uh thankful to have you here by the way where are you broadcasting from
oh excellent yes I am located in Atlanta Georgia uh the United States uh
southeast United States and Atlanta Georgia um it’s lovely here uh it’s very warm uh
highly recommend it um and I’m also going to give us an introduction to this talk that um I’ve
been using Rost uh solidly for about 14 months now
um and before that or even concurrently with that I have been a data engineer in
my uh active life and so um when I first came to rust one of the
very very first things I wanted to do was uh what I typically do which is read
data from some uh rest API and and use it to uh do something
the the something is is largely immaterial because a lot of time it’s read the data from here and write it
back out to there or transform it or do some math on it but but you know it
seems like that’s a very very frequent use case and I wanted to start with that because that’s what I did all the time
um so uh using vs code for rust development and debugging I’m going to go over that as well and uh yeah how we
how to use the turbo fish and love anyhow um that sounds great that sounds great
uh Elliot I’m going to share uh your slides now and I think with that it’s
time to get started uh the stage is yours we are looking forward to your talk I will be in the backstage so if
you need me just let me know otherwise I will be back at the end of your talk to do a little bit of q a i will relay all
the questions from from the audience to you and we can have a nice little chat and discussion at the end so enjoy it
thank you for being part of the rustlands Meetup group and the stage is yours uh thank you again Rainier
um and thanks again for having me uh so just to start or before I really get started um I did want to invite everyone
to enter for a chance to win an apple home pod mini uh my employer Jay frog is
a platinum member of the rust foundation and would really like to give you a chance to enter and win uh the the
homepod mini um you can enter with uh the QR code or this uh jfrock.co Link at the bottom and
I believe Rainier is going to put a banner uh on the video as well um a winner will be selected within
three business days and will be contacted uh via email
um so uh I I wish everyone the best of luck with uh the Apple homepod mini
those are those are nice um so about me uh my name is Elliot Frisch uh at jfrog our core competency
is helping developers Leap Forward I’ve been a developer for over 20 years and writing rust code for a little over a
year now um I have some links here to uh various information about me as well as the code
given in this talk uh which can be found at the the link at the bottom there to my GitHub
um so uh we’ll start with the development environment um I’ve been using vs code with the russ
language server and a number of plugins uh this is fully documented in the readme at the the GitHub
um but uh we can go over it real quick there’s some Russ language server uh tab
nine uh fonts and turning on ligatures and things of that nature for a pleasant
environment and in my terminal I use oh my Z shell and then of course I I walk through how um I’ve set everything up
within this uh talk including uh you know setting up a web server various
crates that I’m demoing um so to very very start uh we’re going
to start with a very very simple Json in fact the simplest Json I could even
think of which is a very simple uh version string uh with a numerical value in it
um so we’re gonna we’re gonna parse this version string um so we’ve got we’ve got a version
we’ve got some numerical value in it and um we’re gonna use the Json parse
crate and of course we need to match because that could return an error uh on
the handle or it could be an okay and if it’s an okay then we’ll actually get a Json value
um and uh we can then display whether we we got that value or not and I’m going
to actually uh switch from here
I’m going to share [Music] the
the S code now so we can actually look at that code and practice
um why is
I have too many windows open I apologize folks
why is this being so difficult right now I’ll just share my entire screen I
apologize all right so here’s the exact same code
but now in uh vs code so that we can just run it
so if we execute this code you’ll see that it it parses out whatever version you put in here and it doesn’t matter if
it’s a string or not because um we are using uh a parse function and a
Json value and so that’s an enum type that can represent multiple things
and we are actually parsing it uh as a F32 so as long as it’s an F32 we’ll get
that back out um and if it’s not of course we will get an error that it
could not parse the empty string so that is how that code functions and we can
then move on to a more sophisticated version um and actually introduce anyhow where
instead of let’s go back to this first version so in this first version where
we’re parsing it there’s quite a bit of of logic that has to go into the to the
matching conditions to determine whether it was a string whether it was a number whether it was a short because again
this Json value is an enumerated type and when I was first starting out this
explicit matching seemed like it was excessively verbose and
um so I spent a lot of time working on how I could reduce that and the answer turned out to be anyhow where we can
instead of matching on all of the error conditions simply use anyhow to wrap
them and this will instead of requiring us to match all of the various possible
Json values we can simply say attempt to parse it as a floating point there as a
F32 a float 32 and if that fails then wrap the whole thing in the anyhow error
and otherwise uh return an okay um so this significantly simplifies uh
actually parsing our Json so if we go into the V2 and we change this to
value and run it um and of course successfully parses it and
if we move the version and run it again
we’ll get the same can’t parse an empty string so so far so good uh anyhow allowed us
to eliminate a whole lot of boilerplate from version one
um so that was excellent so now what can we do well what if we actually went and
got some Json using a uh request blocking call so this is going
to use the HTTP request Library it’s going to make a get to a URL it’s going
to get back the text of that and then it’s going to attempt to parse it um and the URL it happens to be this uh
this string here and so what I’m going to do
three
if I can just start up a web server and I have a version Json in there and
now
instead of uh parsing a simple static string we’re going to actually
get that Json and parse it directly um
being a little slow sorry
there we go so we got a value of 3.15 yeah let me quickly interrupt you I’m
very sorry uh we have a question from the audience if you could increase the font size still a little bit
um if that would be possible that would be very nice ah very good thank you very much and let me just move this back over
I hope that’s clearer for everyone and just to remind everyone this code is
fully available in GitHub it’s also uh on those the slides that have already been shared out so
um yeah I apologize if you if you still can’t read it um I’ll do my best to
to to expand on it um so uh if we if we now go back over to
uh edit that Json and the um V3 uh Json
folder so we’ll just bump the version here to something like 8.15 why not
um and now run it again we’ll see we get 8.15 so it is real code it’s actually
making the the HTTP request it’s getting back uh Json and it’s parsing it so so
far so good well what else could we do well what if we made a call to an actual
API out there this is something called zipopotamus and
um it will return kind of like we were looking at before where we get uh a floating point value
as a string and then have to parse it this actually returns floating Point values and strings and we have to parse
them so uh you know we’ll we’ll start with a default zip code we’ll make the
request uh we’ll get a Json response back we’ll match on that if it was good
we’ll try and parse it so let’s look at what that parse would do we’ll look for
places we’ll get some uh geolocations out of it we could actually use this geo
utils location create if we wanted to but I don’t see the need instead I’m just going to build a Google map
string based on the latitude and longitude based off of the ZIP code that we retrieve so that we can then uh see
it on a Google map so that’s a simple simple use case um and uh so we can go into the V4
folder and run it and I will give it uh the zip code of where I actually live which is
like I said Atlanta uh suburb technically but Atlanta
and we’ll let it run
that big so you guys can see it again
and sure enough we get back a latitude and longitude and a Google link and if we open it
there’s a Atlanta Georgia well technically Norcross Georgia
there it is um and if we simply run the same thing
like I said it has a default so if we run it with default
you get a very unusual location for a US zip code this is the Federated
micronesian island of Yap which is in the middle of the Pacific Ocean
but it’s got lovely beaches so and look at those it’s very nice
um so we can move on uh to the last version
that I’ve implemented here and um
uh we could have several different URLs and we could make all of these calls and parallel and uh
iterate through them on success uh so that’s that’s kind of what this this code’s doing it’s just kind of a a reach
but it’s making three different API calls in parallel and getting the results back and dumping them
um so that was just kind of a strategy of what else could it do
and obviously if we were to execute this
you’ll see that it just prints the URLs the statuses and the text results pretty
quickly um I uh will now go back to presenting the
actual slides so we can go through these
foreign so again uh now I’m gonna Pro really
stop and ask if there are any questions um currently working on uh Persia which
is an Open Source rust-based service designed for securing the software supply chain uh you’re well everyone is
welcome to join us uh there’s our our main project website there’s some more information about me and then of course
uh thank you again uh and anybody just a
reminder who wants to win that Apple homepod mini is welcome uh with that I’m going to turn it back
over to uh Rainier and ask if there are any questions
hi Elliot okay thank you thank you very much let me quickly remove that one okay that’s fine
um that will squeeze that was quick that was fast that was a a quick walk through
um you closed your screen pretty quickly maybe you can you can re-share the visual studio because we have some
questions here from the audience um who would like to to know one or two
things uh the first thing um that um let me share it here okay the first
thing that you didn’t explicitly mention but kind of um raise people’s interest if you take a
look at one of your methods where you have um where you have some condition
that maybe fail you have these question marks in your methods
um where you for instance par is the Json can you elaborate on that a little bit especially in conjunction with
anyhow what do these question mark here do and maybe how do they relate to other
programming languages for instance um
so anyhow is a is a library that extends the standard result and allows to wrap
error handles into any how results instead of standard results and the
question mark specifically is anyhow uh allowing you to do uh that wrapping so
essentially this says if it succeeds uh you return through the Okay match and if
it fails wrap the error in an anyhow error so that we don’t have to handle it
at the explicitly in the parsing function so if we if we were to compare
the very first version of my parsing uh code it’s significant
and I probably should have gone over this in a little more detail but when you’re when you’re matching the the Json
result which is a Json value it’s an enum type it could be a string it could
be a number it could be a short and it could be um
some just just it could be something that it doesn’t make any sense at all it could be invalid Json right
um so when you’re when you’re attempting to match a a a a an enem type like that
you explicitly are going through all of these things saying well was it a number was it a short was it a string and for
each one um having to then say Okay um well then I want to parse it and
check to see if it succeeded and so that’s what all of this code is actually doing is it’s saying okay well in any
case I just want to parse it and see if it was a f-32 well you can shorten all of that and and that’s what I was kind
of trying to demonstrate by using anyhow um and instead of saying I want to
explicitly match all of the possible error or or other conditions
um simply assume it’s going to work and if it fails wrap the failure condition and return that and in a in a basic
error and so instead of having to write just all of that matching syntax it
becomes just a question mark at the end very nice that explicitly is anyhow um
there’s a related library for when you’re actually writing libraries called uh this error
um by the same folks so I should mention that as well okay got it I think in your V3 or was it
the V4 you even had two question marks in a single line and I especially liked
this example because uh it it demonstrates how powerful this this
question mark thing is yeah here we have it in line 24 here you have two
different situations where an error could happen and you can simply write question mark in two places and that
demonstrates very nicely how error handling how elegant error handling can be using rust don’t you agree uh very
much so and I appreciate you for calling that out because yes that is exactly the
um the the the the the the direction I wanted to go with this because again
when I first started and I suspect when most folks first start with rust you start by saying I’m going to explicitly
handle everything yeah whatever it throws at me I’m going to handle it because otherwise I’m going to get a
compiler error um and those are awful and I don’t want to deal with that and and like I said
you end up writing what looks like a lot of boilerplate code and then through learning and uh interacting with
the russ Community uh you start to evolve into these uh more effective
patterns and I think the anyhow pattern is is a very effective one so yes got it
got it we have another question from the audience um um or we have this question let me see
if I this one um the question is if you return an
anyhow error how can you handle the error can you write a match statement on
top of your for instance get Json to do some error handling on the caller side
so now the question relates to not the error inside of your parsing but uh yeah
I think that is what was the topic yeah and and indeed you can
um so when you um yeah right right here when when it gets
the the result so we’re assigning this version result from matching on the
parse version so it’ll either be an error or it’ll be a success and so
matching on that version result if we succeeded we’ll get back uh an F32 which
is a floating Point 32-bit number but if it failed for any reason these are the error how any how errors as well then we
will enter into this error handling match right here and uh that’s where it would happen
um now to actually get the underlying error you certainly you you there there
are additional methods you can use to inject context into the anyhow
um error and this error as well um so if you if you actually need to manipulate the error message you can do
that as well um from your experience um if people have now more detailed
questions they they now saw any how is and and they want to learn more is there
any how documentation a good place to start I very much so I all of the rust
documentation in my opinion has been uh phenomenal but the anyhow especially
documentation is very good in fact if we were to go directly to uh the anyhow
break uh D naught
I’ll just Google it not mine
okay well Google’s trying to be very helpful um we’ll go we’ll click on it this way
it’s down at the bottom so if we go directly to this crate um you can see right here that one of the very first
things they talk about is using the question mark easily propagate any ha error that implements the error trait
so um yes I think their documentation is excellent I would highly recommend People review it okay okay that that
sounds very interesting um one additional question is here on my list you mentioned in your talk title
the turtlefish absolutely let’s let’s go straight back
to here and let’s look at um right here this is a turbo fish whoa
so we’re we’re doing a parse and because we’re doing a parse it could return any
number of different things um and the the rust compiler can
actually infer the type in this circumstance probably but I was explicit and I said I explicitly want a floating
Point 32-bit value and this is a this right here the syntax colon colon less
than pipe greater than is a turbo fish um so yeah that that was kind of hidden
there all along sorry about that no problem that’s that’s perfectly fine I think this is very important uh I always
see in trainings when I do trainings in Rust I see especially people coming from other languages struggle a little bit
with this syntax not being aware of the fact that in Rust a method like parse
does not the name of the method does not include the type that is generated it’s
not like in jaw like in JavaScript where you say parsint or like in C sharp where you say int dot parse in Rust you just
say parse and in some cases the rust compiler simply doesn’t know what should be the result exactly like you just said
and then the turbo fish is very important because you can explicitly give a type annotation to the rust
compiler and then it knows okay I have to call the F32 implementation of parse
in order to generate an F32 and here the turbo fish is super interesting by the
way Elliot do you happen to know the website purple dot fish I do and there’s
also a GitHub where you can download their source code really maybe you can quickly open
turbo.fish just so that everybody who who maybe don’t know this website just knows it because now you see this you
will never forget it again I will show on this website myself um some some time ago I I was astonished
by it because it’s so so nice um ihar is questioning is is asking so
terrible fish is a a type of a generic function um let me show you the the question here
um ehar it’s it’s um it’s not the generic function here in in fact the the parse method comes from
a trait and there are many many implementation of the parse methods for different traits so you have a parse
implementation for in 32 you have a parse implementation for float32 you have a parse implementation for unsigned
32 and so on and so on and by just calling the method rust cannot know
which version of Parts should be implemented should be called it’s just a method that comes from a trait so uh
using the turbo fish is removing and ambiguities
things that are not clear I’m not sure if I’m using the right English word here um it it clarifies to the compiler what
it should do that it should explicitly call the implementation of this parse method from this very type
that that is a hundred percent correct and the alternative would be um you can be explicit on the left hand side and
then it can it can infer it so that’s the alternative so by by explicitly saying I’m expecting
a result that’s an F32 um I can omit the turbo fish or I can
explicitly include it and the parse method call um
so either one we have another question here that is
now a follow-up question um uh what trait does maestro should
Implement in order to be able to be used with pars do you want to give the answer
Elliot or should I um sure I believe the answer is the from string exactly it’s a fromster trait the
frogster trait um uppercase F of uppercase s has to be implemented and that’s uh implicitly
used behind the scenes if you use the Paris method of of stir yeah exactly
exactly yeah very good questions thank you very much okay um did we forget anything are there any
other things that we that you would like to to tell our listeners I think it was
a very interesting talk thank you very much thank you so much for having me I think we covered um everything I wanted
to cover but obviously I wanted to make sure to leave time if there are any other questions so if there are no other
questions then um I will yield the floor back but again thank you so much for
having me it has been a genuine pleasure I look forward to the rest of the speakers very nice very nice thank you
very much Elliot no I don’t think I have any further questions right now uh just
one question will you be able to share your slides and the uh and the code with
us I think you do right will you post it yourself on the on our Discord server or
should I do that for you um if you wouldn’t mind doing that for me just so everybody will be confined yes but that is no problem you already
sent me the slide so I will share the slide deck and now everybody knows it if you are interested in the code that
Elliot showed us today and he would like to Dive In Yourself uh in these code samples then you can do exactly that by
taking a look at the presentations and the links which are embedded in edius presentation
and feel free to Fork the repo thank you very much thank you thank you Elliot thank you for being with us and
thank you for sharing your presentation I wish you a very nice um day is it day is it morning for you
what is the time this afternoon but then afternoon okay it’s evening for us here so enjoy the rest of the day thank you
very much for being with us bye
so this was our first speaker and now we have our second speaker and uh hi
hi Stefan I don’t think I need to ask you where are you broadcasting from because yeah it is close to you
actually exactly hey awesome that you take the time and you took the time to prepare another
presentation and to give it here at the rust Lins Meetup
um I think you were really looking forward to giving this presentation here at roslings I I remember you talking
multiple times about hey when we have when do we have time because I think
this is a topic that is very near to your heart isn’t it yeah so it’s a fun topic I already have written about it at
one occasion and it’s one that uh when I do workshops Etc it’s always a nice example to pull out and present to
somebody who is not so familiar with the powers of a type system so it’s uh you know it’s it’s
it’s it’s kind of my my my my my thing to talk about type systems to talk about fun things that you can do with types
and um yeah that’s um that’s why I’m really excited to talk about this but you know excitement to
talk about this in time to prepare everything those are usually two very different things so I’m I’m really
curious how it will turn out for the first time um I’ve I’ve prepared a lot of examples a couple of slides
um and uh I hope everything goes as smooth as I’ve I’ve planted so yeah let’s see oh I’m I’m I’m very sure I’m
very sure should I do the presentation for on on um on stream yard for you or
will you uh handle it every time I just share my slide so you can um you can pull it up so that’s cool okay and I
hope you can hear me all right soon and not only do I have new slides new examples I’m also trying out the new editor today having a new light a new
camera and new microphone so everything’s new everything can fail so if if anything goes wrong it will be
most likely everything so let’s see how this does let’s not hope for Murphy’s
Law right no no absolutely but you know that’s that’s the pleasure of having a meet-up meetups I see all meetups always
has a stage to try out new things and absolutely so yeah that’s that’s why you are all guinea pigs today for me yeah we
we love to be okay Chef on the stage the stage is yours we are looking forward to
your talk okay thank you very much Hannah all right so hi I’m I’m Stefan and I will talk to you today about the
build a pattern and type stage programming and before I start maybe let me look real quick so
um before I start I want to ask you if you recognize this image
it’s called swans it’s from 1956 it’s from MC Escher who is a very very known
artist and I love his name or their name because I guess MC are are the initials
but I always think of a techno club’s master of ceremony when I read this name
um and uh yeah so this this image um those swans flying in a pattern are
also produced on on some sort of pattern is the cover image of a book of the book
called design patterns elements of reusable object-oriented software by the
eponymous gang of four so it’s right here I hope you can see it
um and this book is now almost 30 years old so it has been published for the first time in 1994. uh it has possibly
sold over half a million copies because it has really defined how people think of object-oriented programming a short
disclaimer I I learned about object-oriented programming of course in in University I
um um I have done my fair share of obstructivity programming in the past I wouldn’t
I wouldn’t help for myself that I’m an expert in object-oriented programming but this is all part of this talk because I decided to look a traditional
object-oriented programming and see how it holds up in Rust um in those design patterns
um um this design patents book um defined 23 Classics of the design
patterns with examples in C plus plus and small talk and University teachers all over the globe rejoiced because they
finally have enough to put on the slides for their curriculum this is where I learned it so I learned it at the
Lindsay University software engineering 2 object-oriented programming and we did all of them in detail but in a Java
script in a travel context though uh the book splits up design patterns in three categories so first they are
create a creational design patterns they sent around the creation of objects rather than instantiating objects
directly there are structural design patterns they are concerned about class and object composition and last but not
least there are behavioral design patterns um which are which Define communication
between objects there’s one behavioral design pattern that you should know when you’re developing rust which is the
iterator so iterate is a classically designed pattern and here you can see how they hold up in Rust it’s also worth
noting that the gang of four the four authors
I hope I didn’t put to his name um that they claim that they have not
invented design patterns that they are not A New Concept and they were present in software development for ages but
they were the first ones to collect them to talk about them to give them names so p people have a common vocabulary for
discussing design patterns and this is why this this book is also so impactful in my opinion because not only has it
described lots of common things in software development but people were able to talk about that and I guess this
is why to this day this book is one of those which is still recommended um even though you know it’s um that the
way it’s written is you I guess you wouldn’t write a book like this today but you can still get a lot out of it
and again I tried to see how do they hold up in a modern day programming
language like rust this is a little side project of mine which I call gamers radiation
gamma because of every Gamma One of the authors of the book in the radiation like okay if if his ideas radiate to
this time how far could we radiate so it’s it’s a little little play on words
um and you know how can it be the how can those patterns be developed in Rust you know that rust has many influences
from other programming languages like C plus plus also influences from other Pro object-oriented programming languages as
well and the Rust book um this one here I have it here as well
the last programming language which is the the paper copy of the one book um that you find online
um also discuss this object-oriented programming and the funny thing is um it discusses it with a quote from the
design patterns book so this is now a quote section because this quote here is
quoted from the rasp programming language which quotes it from the design patterns book in the talk about design
patterns um and it says that this book at that object oriented programs are made up of
objects yes okay and then object packages both data and the procedures
that can operate on on that data and the procedures are typically called Methods
or operations and well we have that in Rust you know if we boil it down if you boil down object-oriented programming to
that we have structs and in-ups and we have input blocks so yeah with that we have some way of object orientation it
might be a little bit different then you know it from well languages like Java because rust lacks inheritance and
inheritance is a fundamental Concept in Java um and you also if you if you think
about implementing options against an interface yeah you can do that with trades but you also need to define the
relationship of a trait like to memory like is it just some marker for monomerifications or trades through
generics or are you using trade objects which is a fundamentally different thing in two fundamentally different
programming Styles so yeah we have object orientation which means it’s good to revisit those old patterns
um of object oriented programming and one patent that I decided to start with
um well next to the to the iterate pattern obviously is to build a pattern because this is one pattern that I
constantly stumbled upon so I’ve seen that in libraries I’ve seen that in the standard library and I’ve implemented a
couple of builders on my own as well it’s a creational pattern and if you do it right it’s also a recreational
pattern um and it’s in chapter three of the book the first chapter of the design patterns
catalog and it’s the second pattern already so it’s it’s pretty pretty early into in the Builder pattern is for object
creation it’s a creational pattern and it says that you should use this use it when the algorithm for creating a
complex object should be independent of the parts that make up the object and how they are assembled and we are going
to see a couple of examples for that and also the construction process must allow different representations for the
objects that’s constructed well this is you know a little bit about um polymorphism and we are not going to
touch that we are only talking about the creation path about the assembling part and you know it’s it’s a book that it’s
loved by University we have class diagrams in there so um this is something where you can fill up your
slides like I do right now and you have a couple of different different components in this filter pattern so
first of all the Builder and the Builder is in this case an interface so if I remember correctly for my days back in
University if it’s italic then it’s just an abstraction of the concretization
um in the Builder specifies and abstract interface for creating parts of product object then here we have a concrete
Builder which is an implementation of Builder and it actually constructs and
assembles parts of the product by implementing the Builder interface so it defines and keeps track of the
representation it creates but it also provides an interface or a method for retrieving the product so this is get
result in in that case and of course then you have the product the product represents the complex object which is
under construction and the concrete Builder builds the product’s internal representation
and also defines the process baby to disassembled and last but not least you have to direct it because everything
needs to be an object well what it does it it calls the build and it assembles part so you can say big part that build
up this yada yada yada let’s see how it works in most so in Rust you are stumbling upon Builder
patterns if you look for example um at hyper so hyper is decreed for
doing HTTP requests and responses you can Implement clients with servers with it if you have done anything web related
in Rust you have used hyper in one way or another um and Hyper has a wonderful way of
constructing requests um and you know in in terms of request it makes a lot of sense we are going to
see why um and this implements the Builder pattern we call or we create the Builder by calling request colon column Builder
it gives us an instant of the request Builder an instance of the request Builder and then we have a fluent
interface where we can specify different parts of the product which is a request we want to get the request out of that
so we have one method to Define well the method get post put delete whatever we
can Define the URI we can define a header and you know we can add more headers this is a funny thing about Builders because we can say well I don’t
know how many headers I’m going to have why would I need to specify everything upon construction I have them stored in
some variable in some binding let’s move them over to the Builder and this is a very flexible part but in in terms of an
HTTP request this makes a lot of sense you know you don’t know how many headers you are going to set
um if you if you you know deal with Dynamic requests for example and also here the last thing and this is
beautiful after we have defined everything we call Builder dot body which gives us the product and the
product is the finished HTTP request and the body uh the body method call is the last method called we do so after that
our object is constructed and this follows you know um the the pattern of of an HTTP request
we are defining method in your right and we’re defining a lot of headers and the last thing we defined is the body and in
theory you know um up until that point uh hyper could think of well maybe we’re
already sending the first information like get URI header and only when the
body is being set we are we are closing the request so you can do a streaming HTTP request and the way this interface
is constructed it allows you to stream on right away so this is a nice noise I don’t know if they do it like that but
the interface would allow it um and yeah so every step is concrete and if you think about all the variants
in HTTP request can take and all the variations that you can do
having an interface like that is beautiful and this leads me to a couple of implications so first um setting
headers can be either very easy or very complicated but the the interface of the Builder is prepared for both
and in this example I did set an HTTP method but I I don’t have to because
there’s a default HTTP method which is get so I I don’t need to Define an HTTP
method so I also have the possibility to to default on a couple of of
um yeah values for the properties that I set and then if you think about having all
this complexity and you think about how rust defines function interfaces and and um methods
rust has no variated argument lists for functions think about print Line Print
line is not a function it’s a macro wide because we have a very addicted list of arguments and you can’t set default
values for functional arguments so if you if you use a Constructor function like colon colon U which is funny enough
a pattern on its own it wouldn’t work as well and we can see that in this fictional representation of
a standard process common so this is not true this is something that I put up a command is exists in standard process it
is used to spawn a child process where you can set the command some arguments environment variables you define
standard in standard out and standard error and you can have a working directory and the working directory is
funnily enough the only thing that is optional every other thing all the other things need to be said uh indiff I’m
doing um a standard Constructor function where I’m going to set all those values you
see on the right hand side this is the new function um and everything is optional except the one thing that I actually need the one
thing that I actually care about and in my implementation I either say unwrap or default unwraport default or I set it to
IO standard in iOS 10. iOS 10. so I’m setting already a couple of default values and this Constructor function is
really really inflexible if I look at the usage look at that so I have ls.into then maybe l and a as arguments and then
lots of none and I don’t even know what all those nouns mean so this is something where I say well this is a
really really bad interface to use for something like a command for something that spawns a child
process I mean I could instantiate it directly like having having um distract
augmented common to LS and then just go for default you know if I implement the default uh trade for command I can do
things like that but then again you would need to know a lot about all these properties and the more complex the
object or the instances the harder it gets to say well just to default default
um and maybe you really want to know what’s behind it or you want to have a little bit of flexibility if we look at the actual implementation
from Standard Process well the user build a command becomes a builder they say new LS so list everything with the
argument L with the argument a and then spawn the process cool that’s the one line it’s beautiful and it when you read
it you know exactly what it’s doing and if you have something more complex well for like the optional working
directory you can say well let’s do a new command let’s set a couple of arguments and then if I have a working
directory well then please set it and then spawn the command so again also also from the construction
here again it becomes obvious what it does and it’s prepared the interface is prepared for everything a really really
beautiful interface and something you know um I get I get pretty excited about interfaces like that if I look at
um this example from the talks so I can I can say either I do something on um on
Windows on it’s not partial I guess it’s just command we have a couple of arguments
um or I call it on bash you know um I can decide it in in an if condition
whatever but the beautiful thing here is actually the output so I’m creating a
different kind of object once I call output I don’t correspond because spawn just sponsor process but I don’t do
anything if I want to get the output or not and with DOT output it executes it and waits until it gets some output
which I can read in afterwards and I love those kind of things because it’s a really really nice API I can I
think can basically construct my command entirely and then decide if I call output if I call build was born
depending on the context where I am and this is also flexibility that I get from a builder pattern okay
a little bit more about build dependency just because yeah well this is something from the standard Library some something from from hyper but there are lots more
builder patterns in Rust and usually we differentiate between two variations
one variation is a non-consuming builder and a non-consuming builder means it is
um respective to the ownership of the Builder structs so um command is a non-consuming build
if you look at the implementation this is abbreviated because it has much much more or many many more methods
um I have um a struct command input block creates a new command and with
every Argo arcs that I add um I’m adding something to uh to the list
um and if you look at that both Arc and ox and also all the other methods there use a mutable reference to itself which
means that the actual instance is going to be changed and to get a fluent interface so you can call Dot
arc.arc.arc.arc it also returns a mutable reference to itself uh which
means that if you if you call that Arc if you call to them it changes the actual instance and so on and so forth
and this is non-consuming you don’t consume the actual instance um you are changing the instance
and this has a couple of implications so if I call it for example like that they say let’s command is common color column
U with one argument the return type is not command anymore it’s mutable command so it’s a mutable reference to to
command and difficult spawn in the next line I’m getting an error message because well I don’t have an owner so if
I call command new DOT Arc I don’t have an owner the owner is transient
um and if I have a borrow later on it doesn’t know what the owner was or what the owner is and um the rust error
message is actually quite nice thanks Esteban by the way because it tells you what the problem is and it also tells
you how to solve it create the new Builder store it in the binding and then
do all the other changes so you would need to to assign it then again to binding.org and send it to command
um but you need to have the first instance once you create it with new you need to store it in a binding to
continue with it and of course you can still do one minus so one liners are still still totally
fine on the other hand you have consuming buildings and this is also an example from standard thread Builder so you can
create the new Builder a new thread via this Builder where you set the name and optional name and can set the stack size
but if you look at the interface this is also abbreviated again to fit on a slide you see that they take a mutable
a mutable owned struct so not the multiple reference but an own struct and
also return and own struct which means that if you are changing methods within your control flow
you need to reassign to a new binding after each method call
one-liners or chained methods still work as well but if I would call here thread
dot stack size without an assignment a thread would crop so so it it would
not change the actual instance it would produce a new instance and the original
instance would be dropped um so yeah one liners to an extent still work and those are the two different
different kinds usually um there are circuits is that consuming non-consuming Builders are preferred and
you really need to have a reason for consuming builders so and this leads me
um to the second half of my talk types the programming but now I’ve seen what the Builder actually is
um and now I want to play a little bit with interfaces um Builders have an actual use cases in
Rust we’ve seen that good use cases is that um but I want to know if it’s easier or
if I can steer in some way how my products are constructed and it should be simple to my users and it should be
elegant and it should be fun um and what I want to create is again it’s it’s always my example maybe I’m
biased here but I want to create a serverless platform um uh in this serverless platform
creates execution environments where you have a worker a worker process that contains ad workload the one thing that
you want to execute some allocated memory and the flag if the worker should be kept alive after executioner should
be terminated so you know this is the typical cold start in serverless where you’ll say well I put something up
and then please keep the instance because you want to send the same workload over and over again but I want to use the same parameters
um and you know to execute everything to to spawn a new worker or to build a new worker
um you need to have all property set you need the workload you need the mem size and you need keep alive
however only memes size in people life can be pre-filled with defaults you know
you have some default settings and you can change them later on whatever um but there defaults the workload isn’t
and the workout might come from an external source so you don’t know when constructing the instance if you already
have a workload ready or if the workload comes from some stream or some request or whatever
and this Nuance is mirrored in the worker Builder if you look at the worker Builder it has the same properties as
the worker but it contains an option of a string so the workload is not yet defined either we have a workload or we
have no workbook and this is by the way also a great usage of a worker because you know you are assembling parts you’re
constructing things with with the Builder and if you don’t know about certain things yet well then you have
have an undefined value or you have an a knowledge value in other programming languages in Rust you have the option to
a value and you can make sure that only if the workload is set that you have an
actual worker at hands that has everything it needs so the the Builder
is a great way to get rid of those optionals if you don’t need them
so yeah a little bit of a different API and that has some reason so now let’s Implement a very rough
implementation of a worker Builder so first of all we have the worker build up here we’re creating a Constructor function it has no arguments so we are
say workload there’s no workload we don’t have one defined yet we create the memory size this is well in megabyte I
guess uh 128 megabytes and we don’t want to keep it alive so the work is
terminated after execution and then we have the workload method so this is where we set the workload method
um it’s a mutable reference to myself which means it’s a non-consuming builder and workload impulse into string which
means that um anything that can be converted into a string I take it this is a beautiful way
of monomorphization beautiful way of working with genetics where just say well give me whatever you have can be a string slides can be an old string can
be anything that implements to string or display doesn’t matter as long as you can be transformed into a
string I’ll take it and then I said well I set this double click tool this workload I called
workload.in 2 to go from what it had to an actual string and to return myself this is how I get this
uh chainable interface um I dropped the methods for mem size and keep alive they’re pretty easy we’re
going to see them later anyway okay so this is how I set my workload and then I have here to build function
where I say well I create the worker and it has the workload I call unwrap because hey I’m sure that workload has
been set um I I transfer memcers I transfer keep a life and have this worker and actually
this should be just self or consuming a method because after this
step I want to consume the worker Builder so the worker Builder can’t be reused again after the worker bill
determinates I want to be done with it so this is an error in my slots and mirrors are about that um but here’s one problem so I have this
scared scared Emoji uh right next to it because hey I’m calling unrepute this is
an answer for operation this might Panic if I didn’t set workout everything might go boom and I want to avoid that and I
want to avoid it well I have a couple of possibilities there first I could I
could just say well you’re not getting a worker you’re getting the result of a broker or maybe a construction error
result types were beautiful in that then you know you have to uh you have transformation methods from an option to
an error and back would be a beautiful way to develop but well actually why would I even allow to call build if
I don’t have a workload set and I can Implement that stuff I can Implement that through type state
let’s see how I do it first I change the worker Builder interface so I say the worker Builder
now doesn’t have a workload which is an optional string but it has a generic parameter it is a generic parameter
where I have not decided yet what the workload should be and then I’m going to implement the
generic worker Builder those are all the methods which don’t care about the state
of my workload because the workload has a state either it’s here already or it isn’t but those methods for memsizing keeper
life they don’t care about the state of tubercle so I can implement it for the generic version of worker Builder and
this is what I do here so it’s just it just set us you have implemented that and again it takes a mutable reference
returns a multiple reference so it’s it’s a non-consuming builder
but now I start with the case what if I don’t have a record yet
and I Define a struct no workload look at that it’s an empty struct it has no Fields it’s just here to say well
this is one thing I want to I want to care about and then I’m implement worker builder for this particular structure no
generics anymore just for this particular struct and here in this particular implementation for worker builder of no
workload I have the construct a function and the Constructor functions is new it
returns workout which is now workout it fits because hey that’s my that’s my parameter here that’s okay
um the members and to keep alive fine that’s great and I already defined mem size already defined keep alive so
now I need to Define workload and it takes again a workload input into string but look at that now I’m not returning
self or changed something in my parameters I’m returning a new worker Builder I’m returning a new worker
Builder where I set the workload into well this particular string where I set the mem size to self-em size people left
to save keep lab and moving those fields over to the new worker Builder and if you look at the at the function
signature if you look at the return type and now not returning self I’m returning
a worker Builder string so I’m having the same struct the same type but I have
a different state of my generic type so I’m moving from no workload to string
and you can really think about like about it like a state machine so you started mobile code and when you call
workload you are moving to a different state so let’s implement the rest let’s Implement worker build or string here I
have to build method and again sorry this is this is wrong this shouldn’t be an ampers should be just self and these
ones since now I’m I’m 100 sure that I have everything that I need that I have
um a workout I can simply construct a worker with the workload with mem size
in keeper life and return that
and yeah that’s the last state and the beautiful thing about that
workout has a different state vehicle has a state where it starts which is nowhere close and then it has a state
when it ends which is I’m having a workload of type string um and those are two bookends of the
construction process so this is where you start in the Builder pattern you start creating a builder and then you are done with the building process and
you’re getting an extra object or an actual instance of your struct um and and those two bookends are always
there in a builder beta and the one thing in the middle is variable this is the one thing where
something changes and those two bookends are separated by state by the different
implementation blocks so it becomes totally obvious which
state should be the start which one should be the end and you know what um I do think I have
some time left um let me quickly
um switch my screen to
there we go so um actually now I just had a talk and
I’m doing some some slides now it’s getting really interesting because I’m I’m doing new Bim and I haven’t done it
yet oh sorry I should I should add it to the stream damn it so um it’s going to be interesting I hope I
hope I don’t um I’m not I’m not stumbling over myself here but yeah I’ve implemented
everything we’ve just seen here I have the worker I have to struck my workbook I have this worker builder of w
um after implementation for the no workload state um and I’m having here
um I’m having you develop a bit of string okay I’m actually getting the build method
um so yeah um here I’ve also changed something so here I’ve also changed it to a consuming
um uh call because I’m getting a new structure so this is okay um this this works like intended
um and here um I’m having memsizing Kipling so there you go and now let’s try to
implement it well to work with it so I’m having my worker and as a worker Builder
um yeah can you edit it huh colon colon new there it is Works fantastic and if you
look at it I’m getting a worker build of no broker so uh the inlay hints from us analyzer already tell me that I’m having
a workout and I can do things like you know whoop
working with a new editor for the first time it’s ridiculous there you go
um so I can do things like
set mentors and said or keep alive to true and here setting mem size to
um yeah on a 256 times uh this one here and if I save it there
go um and let’s so I’m still having um you don’t see it that well let’s add
let’s add keep alive again so rust analyzer formats for something nice there you go
um you see that I’m having here a backup builder of nowhere close still so so nothing changes here it’s still a worker
build off no problem but um if I’m going to change this to
um so to build look at that um I’m also getting the notice well no
method name build found to start to Acapella so it’s not there it’s not within the implementation
um that I have here um and if I do cargo quilt um it also tells me method not found in
work you build on a workload but the method was found to work a builder string so again thanks is the bump those are great error messages that help me
um with my case um so I go to this line and say well now I’m
setting a workload um and I’m sitting it to an empty string and look at that now it changes from worker building to workload to worker
Builder string and now I’m also having um repeat method available and now I’m getting a workout so this is this is
what I get out of there and you know I still can work with with the generic methods I still can say well let’s have
mem size and set it to something entirely different um or let’s call uh keep alive and
change it to to fault so I’m still having those methods that come from the generic implementation they are still
there because they are generic over all possible States um but the moment I go at the workload I’m
moving state I’m moving state from the concrete concretization of nowhere code to the concretization
um of string and this is how I can use type State programming to make sure my apis are good to make sure that when I
develop something um that my users and everybody who uses my apis
are not doing anything wrong and they don’t have any weird state so I need to take care about results when the only thing that can happen is to have a
vehicle in the end so if my API tells me that I need to have a workload um then uh I need this this pattern or
this way of constructing objects is really really nice and helps me a lot in
the way I work okay back to my slides just one second
and here I go so yeah you
yeah okay you should see myself again there you go um okay
I showed it that’s fine feels amazing so how how far went Gamers radiation
um yeah I guess the Builder pattern holds up really well uh in fact I think it’s perfectly suited for us so it shows
with a lot of examples that we’ve already seen in hyper and the standard library and if you have situations where
you are constructing a complex object that may react to certain things in your program then
well it’s it’s really good to use a build a pattern if you want to construct something
the pro side suitable from one liners so for easy construction but also for complex construction
um it prevents proliferation of Constructors which means well what if your Constructor takes then I don’t know how many function arguments or methods
or uh yeah function arguments um how often do you need to set an option even though though you shouldn’t
um if the Constructor gets more um complex then instantiate that
instantially instantiating it directly I’m sorry then why not using
um then why don’t you use a filter directly it has the complexity of the product and
you know with type step that it can add a fantastic and types of API the con cons are well constructed
functions um or direct construction are simply in design they might be more accessible in the beginning but you know well if you
have simple objects or simple struct then then build a petal is obviously Overkill
and with that I’m at the end of my talk and I’m open for all the questions and
say thank you very much
thank you very much thank you very much Stefan I I so much enjoyed your talk now
I have my my um dinner here in front of me I was eating great pasta watching a talk about
rust in the Builder pattern and I very much enjoyed it thank you and I’m so
hungry if you if you’re talking about pasta right now
I was able to cook my own pasta and eat half of it during your talk so I’m sorry
sorry I should have I should have talked longer then yes you should just kidding just kidding just
kidding definitely we have a bunch of questions uh first yes um people were saying um yeah very cool
stuff so yeah so I think you did a great job um thank you very much I I have to start
with the question why any of him why no longer if nobody nobody else is going to
ask this question I will just uh my midlife crisis now
the thing is I set out trying out new editors um over over Christmas actually
um because I’m currently writing a book and um I found Visual Studio code with this
particular uh um Book Project to be really really slow so I had the urge to try out something
new and I tried Sublime Text again which is nice and also to add newbeam so something that optimizes for Speed
um but I’m as you can see I’m so clumsy with it with the wind bindings I’m much more productive in Sublime Text though
but I say well if I don’t try it I can’t argue about it so this is why I tried it out yeah okay very nice very nice okay
so now to your uh to serious questions two serious questions the first question that I see here in the list of questions
is uh can you talk a little bit about error handling Builder um you see please give an example with
build a pattern with a beautiful brittle and pattern the return result of Builder and error uh in general let’s let’s just
talk about errors that can happen during building something
yeah so the way I would approach it um the thing is now types did actually helps me to
um to make impossible States impossible and I guess this is the goal um of
um um of of rust in general and rust has various ways to do so one thing that
you’ve seen is to build a pattern yes but also other things are option in result uh because option
option result both enums um urge you
um to deal with errors and to deal with undefined states which is great which is one of those things about rust that I
love so much um because um uh um I if if you if you adhere to
certain principles like never using unwrap always dealing with your errors you can write to really really safe code
and you write programs that that don’t have to typically Acres like null Point
exceptions undefined not a function everything that you’ve seen in all the other programming languages so this is why I love result and option for that in
that case if I would have a build method where I have an undefined State an undefined State uh because I have no
work code I would see if I don’t know if self dot workload dot is none that’s
that’s a method on an option I would return an error with your self-defined construction error this is
what I would do here um and if I have one I would unwrap it I
would put it in the work and I would give an okay a result with the actual
recording and this is also a totally valid pattern
um and it’s also really really nice because [Music] um the function signature that the method
signature tells you that something might go wrong here by just having a result as a return type
and you as a developer need to work with it you need to explicitly deal with it
the difference to the typeset pattern that I showed you is that with typeset I’m preventing those situations upfront
whereas in um in in with a result
um I’m preventing them after the fact so I might have results that are not
like I want them to be and then I need to work with them so yeah
thank you thank you very much I hope this answers your questions by the way yeah I I guess if it doesn’t answer the
question please uh let us know I I was just a little bit of um um yeah distracted because my cat tried to eat
the rest of my pasta so uh [Laughter]
um she’s down there she will come up again because she smells my pasta but let’s go back for more important stuff
than just talking about my pasta here um the the next question is one about
macros is there any macro to automatically generate types for Builder
States this is a very good question I something like this yeah I don’t know of any actually
um I it wouldn’t bother me if there is anything because usually you you get
crates with macros everywhere on craterial so but I don’t know of any so
um but you know my Approach is usually to to to try out things like the like the work under the hood I also when I
teach error handling for example I never use anyhow anticero what we’ve seen with Elliot I’m I’m usually writing them on
my own because you get a better feeling of how how something is constructed and not in production code obviously and
then I’m using Marcus but I don’t know of uh of a builder pattern that does it
like that um it might be because Builders usually tend to be very specific to the product
they are creating and if you looked at at the thread Builder or the comment Builder which builds a child process they have vastly
different apis it’s just that the pattern like like the implementation and the purpose behind
them is similar but they don’t adhere to any um convention for example
which is um which is actually which speaks again you know if we say how how well did
those patterns hold up it speaks a lot for those patents because to have to have a pattern that
can be used in so many so many situations with so many styles
but it’s still the same pattern and you recognize it I think that’s beautiful so I as an API note I I loved
it okay very interesting thank you very much thank you very much let me quickly
go through the questions again um there were some details about which
were relating to some some lines of code I’m not sure if we can cover that again somebody recognized some Builder
patterns from the clap crate which is a very widely used one do you want to say something about that
um I’ve used I haven’t used clip too much so I can’t I can’t see anything about clapping um but yeah a clip would be a perfect
perfect case for that actually yeah absolutely absolutely so everything else is thanks and awesome talk and great
approach and thank you and show me your cat and I think I’ve seen two two questions
here yeah that’s okay for you yeah absolutely one is this why why do you need to clone
the workload uh build the health workload clone yeah so first because I’m lazy um but and also maybe because I’ve I’ve
used the wrong ownership uh in in the build uh method I already said today
um I took a multiple reference to self in build but I should have taken um an owned self
because then after after build um workload can herself can go out of
scope which means that it can transfer ownership on the work code easy to the worker itself and doesn’t it doesn’t
need to be in the Builder so this was this was an error on my path and I just
realized the multiple signature but not not the other one exactly
um and there was another question uh related to that consumer versus non-consumer move ownership versus using
resverances okay sorry that’s another question but this is related to that yeah
okay okay and I think very nice so now you
need to make another book on using rust patterns in production grade projects so
Stefan I think you can put that on your to-do list
Kanti just reminded me that I I need to extend my ownership of ruststashbook.com
[Laughter] how about that but yeah I have a couple of I have a
couple of topics that I want to talk about maybe I don’t know I want to finish the one that I’m currently working on awesome
I’m never writing a book again but I already said it three times so yeah
very good very good so Stefan thank you very much for the talk uh thank you to
everybody who was watching of course we will publish the recordings of our talks
uh on on YouTube um might take us a few days a few weeks it depends on our personal workload
especially this part on Stefan’s workload so we will see but it will definitely show up on YouTube I’m very
sure about that might take a week so yeah yeah that’s that’s fine I think
that’s that’s perfectly fine that’s perfectly fine so I think with that we
are through for today what do you think Stefan do we have anything else to announce uh I don’t think so good so good I think
we all we all done here we are all set so everybody enjoy the rest of the day have um have a very beautiful evening
and just came in another question which is not related to rust but to something
completely different do you want to say something on it um
on on Discord oh no that’s that’s good so if you still have the time um we can
also check on this code so I’m online if you if you want to to hang out afterwards I’m going to go and get
something to eat but I will answer all the questions that I get on on this code um my upcoming typescript cookbook thank
you very much for the plug so I don’t have to um it will contain chapter 17 mostly how
to get types what they know and how to construct Nano projects um and
um I I love using Dino so I use steno a lot at work we are using it in a scenario that is usually not how you
would write Nano applications uh regularly but it’s very versatile it’s
very flexible it has fantastic apis in writing demo is usually a choice so I I
really love what they are doing um on an API perspective and how JavaScript code should be or typescript
code should be um I wish they were popular but they now announced that they have mpm
compatible bindings which is great so you have lots of dependencies that you can use
um and and which should ease that option quite a lot um and yeah and it’s written in Rust so
it’s super easy for me to contribute and with before ladies so the team that
I’m working with always has done a contribution yeah nice yeah nice yeah I can’t remember which one again but we
have I guess we’ve we’ve made um we’ve made
[Music] um we’ve made one field public that wasn’t public before so I guess there was it so
definitely useful if you need it it’s perfect to have it yeah okay I think
with that we should close it for today thank you very much enjoy the rest of the evening see you next month at the
rust Lintz Meetup goodbye everybody thank you Stefan again goodbye see you bye bye