Rust Features that I Want in C++ - David Sankel - CppNow 2022

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everybody i am david senkel and uh this talk is about rus features that i want in c plus plus uh i am a principal scientist at adobe well that's actually half true i will be a principal scientist and adobe in like two weeks but but close enough right so just the basic format of this talk is we're gonna go over some rust features we're gonna talk about what what they are why they happen to be useful and there's going to be a gap with c plus the meeting this rust feature we're going to talk about how this gap can be closed okay so we're going to cover a lot of features we're going to talk about language variants reflection a whole bunch of different tools for systems engineers concurrency community um and anyway you're going to be thinking like you're biased you're totally biased and you're right i'm biased so let's just talk about upfront what my biases are and you can see if that aligns with uh with your biases and the more it aligns the more useful this talk will be so what are david's biases first one i work on large systems big millions and millions of lines of code systems uh that's that's what i do that's what i've been doing for the past like i don't know 15 years um so uh that's my biases are definitely towards things that will help with that kind of environment the systems have lots of moving parts it's not a large system that's really simple it's a large system which is very complex and this software lives for a very long time it's measured in decades so the any any time someone's working on a code like it's 99 of the time or 90 of the time that they're working on code that was written by somebody else okay that's just the environment that i work in and these systems undergo continual evolution new features are being added they're not systems that are just like in maintenance mode you kind of chill no there there are constantly things that need to be done to these systems in order to continue to keep their market share increase market share that kind of stuff so these are my biases and my primary concerns our first one is the continuous operation of the system if the system goes down that's a big problem this doesn't mean that the systems i need these systems to have no bugs i don't care if it has a bug i care if it has a bug that's actually going to impact a user or something in a big negative way right so there's an important distinction there and even if the thing works properly if a user complains about something that's a problem so i care about that and i also care about time to market and when i talk about time to market there are two different aspects of it one is short term and one is long term so short term time to market these are like hey i need to add a feature and and hopefully i can get this rolled out within a month or whatever the time scale is that short term time to market but i also care a lot about long term time to market because i want to make sure that 10 years from now when somebody else takes over the system that it can be just as agile as it was when i had it so that means i can't have technical growth that just grow and grow and grow in our time to market increase and increase and increase over time uh so i have a long term time to market bias i'd rather you know you know release a feature a little bit later now in order to be able to keep that long-term time to market in a reasonable place so these are my biases hopefully you're okay with that this is how i'm looking at rust so some more caveats i am not a rust expert like by any means just it just happens to be the case that i'm not a rust expert so sorry um i've never done a large scale project in rust like large scale like the kind of projects that i work on there are very few of these out there right now so that's not a surprise that i don't have that experience there's not a lot of people that do so i'm going based on um my experience in large scale development looking at rust and seeing like imagining what it would be like doing large-scale development and rust okay cool so who cares about rust why were we even having this conversation i get a lot of emails do you get a lot of emails yeah i get a ton of emails i got a lot of recruitment emails recruiters reaching out to me do you get those emails too okay so yeah i delete delete but every once in a while i'll kind of pay attention just like you know what's going on in the recruitment world and i noticed more and more uh recruiters reaching out looking for people to uh program and rest right and and just kept increasing increasing in the numbers um and the kind of work that they were doing are uh well first off these were small companies right but they were small companies building system software that they would become the next large companies they'd become the next adobe or the next bloomberg or whatever these companies are basing their software capital on rust right so that makes it interesting to me i want to figure out like what's going on here with this rust thing is worth looking into so some statistics about rust i took these from the 2021 stack overflow developer survey that's i think a reasonable survey i'm sure there's like selection bias but there are 83 000 respondents to the survey that they had which is a big number right so i think they're significant i mean the numbers might not be exact but they're some they'll tell you something the first one is that for every 10 c plus plus pull requests on github there's one rust pull request on github okay there's a lot of c plus software development going on so having one out of 10 and github that's a big number okay that's significant and then for every 3.5 professional c plus developers there's one rust professional developer i couldn't believe that like i don't know that many rust professional developers out there but this is what they got from their statistics and i know it could be off by a factor of 10. even if it is it's still a huge number right so there's something going on with this language i think it's something that is worthwhile that we should look into but it was also voted as most loved language for many years in a row right why is this systems language you know versus all these other languages javascript or whatever why is rust the most loved language many years in a row that just means it's worth looking into so i did and we're gonna basically see the results of my looking into this language here's what rust pitch is from rust's point of view they say we're performance like we care about this we want to have fast and memory efficient applications that's one of their focuses if you want that then you should look at rust that's what they say they also say that reliability is a really important part of their concerns they have a rich type system they have this ownership model uh guarantees we'll talk about that like way later in the talk um and they also have a a pitch about productivity there they have good documentation they have what they call a friendly compiler they have great tooling now this is just what rust says is great about itself my perspective this is i would not say these are the cool things about rust right this is just what russ says is cool about rust so let's uh let's looking to see what i think is cool about rust first one as language variants or what rust calls enums so this is a c plus plus code here right we have this enum class color it has three enumerators red green and blue you've all seen this before you can create auto c is color red and then you can also do c equals static cast color three is that actually is that okay does anybody think that's not okay what do i mean by okay yeah that's a good question it is a good question um from the so so let me just explain so this is totally fine from the compiler's perspective right because when a compiler a c plus plus compiler sees an enum what it thinks is oh like a enum class this is basically an int with three defined constants right but it could be anything else that fits fits within the with an int okay so that's totally cool with c plus plus it's fine people don't use it that way but that's what it is yeah you hope but it's but it's named constance all right so here's what rust looks like the enum looks the exact same can anybody spot the difference between this enum and that enum there's a class anything else semicolon yeah okay but really really similar okay now now the uh now if you look at this line nine we make a mutable variable c so you can probably intuit like what this does you're just creating a variable called c is kind of like our auto and then we assign c to three this is an error in rust in fact there's no static cast there's no way for you to create a value of this enum type that isn't red green or blue it's just it doesn't allow you to do that it's not a int with named constants it's just one of those three things all right that's how it works so um yeah this is what i just said this is kind of similar to a variant right so how do we do this with a variant in c plus plus something like this you create these classes red green and blue they have no data members and then you have a stood variant red green and blue and we're just calling it color uh this works and this you just there's no way to make this variant for it just doesn't make sense a bearing is just one of these three things so yeah you can do that in c plus plus but it's kind of ugly like look at all those classes it's just like a lot of syntax a unique type wasn't really introduced right because we'd have the using declaration there someone could you know make another variant that calls this something else so there are ways to get around that and then it just makes it even more ugly um and then there are i mean just basically like variants aren't supposed to do that right that's what enums are for variants are supposed to have the different alternatives are supposed to have more than one value that's just what what it was it wasn't designed for this so you should just use an enum well enums in rust can carry values so they're very similar to variants in c plus so here we have a game event and here we have player died so just like the rust enum on the other screen there this is just like a single possible alternative this could be then you have key press but this carries a value with it this carries a char with it and then here we have click which carries two float32s with it one called x and one called y okay cool so how do we use this thing well this is just like the simple enum case you just assign something if i want to make it a key press then i just pass in the parameter for what i want the value to be when it's that alternative and then similar for the click event i say i want x to be 0 y equal to 10. so this is basically a feature of rust which i think is pretty cool if you want to get the value out of this thing you have this match uh construct here and here you're just kind of like opening it up and saying like oh if this happens to be a player lost do this thing on the right if this happens to be a key press that is either lowercase q or uppercase q then output q key pressed and just and this is called pattern matching and rust it's it's pretty cool and you can just get a little bit of the sense of the power when you see something like this right you can nest these patterns they can be arbitrarily complex and uh here's a key press for anything else that isn't q and uh that underscores your wild card in that case and again click here we have x and y and now we can just output x and y this is how you use it syntax is pretty compact it's pretty nice so what would you do with something like this there's this library called pull down c mark written by ralph livin so this is a pull-based parser now when you talk about parsers for something like markdown you have a couple of different varieties one would be you parse your uh your markdown into an abstract syntax tree right it's like a dom style way of parsing things so that's one way of doing things it has a certain set of trade-offs it uses a bunch of memory um you're kind of stuck with this ast the other style that people frequently do is more like the sax style so you parse it and then you pass in this big visitor with a bunch of member functions that handle the different cases of what things are like so this is a different way of looking at it so he would call that kind of sax style to be push-based it's pushing information and your visitor just kind of reacts to it so this is a pull-based parser and just to give you a little kind of a flavor of what it looks like try not to get too hung up on like this line one here but essentially what what what this block of code is doing is it says when you happen to see a soft break convert that into a hard break if there's anything else just ignore it right just a little tiny snippet of code that basically filters your your markdown and does a replacement this one here is probably a little bit more uh practical you know if you happen to have teh as a text element in your in your markdown then this will replace it with th e and there's some other syntax here which i won't go into but the key thing to take away is you have these really lightweight pieces that you can compose together in order to do a basic task like markdown parsing i think it's kind of cool so what do i want i want language variant support okay i want rust enums if you have access to these things you'll realize really soon that these are almost as ubiquitous as classes like a giant suck and you'll realize that this is a basic tool this is something in your toolbox that you will reach reach out reach for all the time um we we do things in really funny ways in c plus plus because we don't have something like this and variance the closest thing and and the syntax is terrible i'm sorry it should be a language variant should be a language construct i very much agree yeah um so idioms are great but there is something you miss ver if you only have enums and not the bernstein is when you have this uh let's say you have a big denim with all these associated values like your game event sometimes you want to express a function that just works for one of these game events and because it's not a type it's a case of an enum then you're not able to express a function for this specific game event you need to take the aluminum and a big enough function you would say well i don't care about all of the case maybe i just crashed there and that's um an issue with with the type system you have your type system you you cannot single out a specific element and what's also bad with that is let's say you have you're in you you care about all cases but so it's the lack of a subtyping relationship you're talking about yeah so that's one one issue and a second issue is maybe you have parts of these cases of your inum that have a property or a function but not all and now you you need to have those big switches every time you cannot just use the subtyping variation so it become cumbersome so the comment is that if you do something like this use something like this basically when you do your pattern matching you got to consider like all the values you can't just write a function where i only care about like these two or this one um and that's true that's one of the trade-offs of it um and the other thing that you said i'm not going to repeat that i think it's kind of related um so yes you're that's true but i still want this yeah so i mean dimitri and i are are you know taking this feature to task because we have the same feature in swift and it's it's way better than what you have in cpl plus for sure big improvement but it is it does get cumbersome and it's maybe you might be do better with the with the variant with the subtype relation i think it might really do better uh partly because you can you can recreate this thing using that system so uh the the comment is that there's some work in improving this even better than what rust has and uh and maybe this would be a special case of of a more general facility cool that's great i still want this right as long as i get this at some point like i don't care um so pattern matching so this is something that you need if you have language variants because otherwise you can't like break open the value but these pattern matching is useful not just in like the language variant case it's useful in a lot of different cases to simplify code and make it more readable so this is what i want all out from like right five years on adding pattern measurements oh there's the comment that there's there's been a couple papers out in the last five years on pattern matching in c plus plus by by bjarna yes i i know um i happen to be a co-author of those early some of them so there you go so i i i wrote a paper in 2015. okay this is a while back seven years not fine um this this is when we were discussing um and we're talking about the gap by the way now um this is when we were discussing whether we want to have stood variant in c plus 17. and there was a lot of debate about what the meaning of stood variant should be and all that and i wrote this paper after i thought about it a little bit i didn't know anything about rust and said like this is what i really want um so i called it enum union and uh and basically you put the type on the left and the identifier on the right so it looks a lot like uh normal structs except that the difference between this and a normal struct is you're one of these three different alternatives here and they carry this stuff on the left as the values okay this is primitive right this is old paper um and then for pattern matching i just used switch there's a lot of reasons why this this particular syntax wouldn't work but this was presented to the committee back in 2015. and uh the committee said yeah we actually want this everybody was in favor of it there was only one person that was strongly against and that happened to biarno which is uh unfortunate but but really the committee wanted this i think biannual just needed to learn a little bit more about it um the hurdle here is complexity with getting this in c plus there is like opt-in syntax right how do you get your custom type to be able to opt into pattern matching or opt-in to act like a language variant there is pattern matching itself which is a big topic and then there's language variance which is separable from these two different things so there's just a lot of work that goes into this one of the big issues is the exception thrown on construction piece the same issue that we had with stood variant it also happens with a language-based variant and i don't i don't know how to solve this problem right in a clean way and there's probably a way i just i haven't been able to figure it out and nobody's ex had an idea that has been convincing yet backwards compatibility is is a big challenge up in just a second so using underscore as a wild card was a big issue because some libraries out there used underscore for something you know libraries that people use quite a bit like gmoc and so we're like okay well maybe we can make it like a context sensitive keyword um and anyway it got rejected and the basically what we're left with is like a double underscore as our wild card which is just like oh but anyway i mean it's an example of the complexity that we have to deal with when we're trying to put a feature in like comments i was this uh like just like a while at the end um maybe leaving to know except you know types that have not accepted instructors so so the comment is maybe one way to solve this is only have types that happen to have no except constructors and and there's like various permutations of that as a possibility um so that's definitely one of the options and it's totally about i don't want to go into that here um because it's a big topic and looking at the alternatives and the trade-offs uh but we should definitely talk about that later for sure the other thing is that there's competing visions for the design so we were pretty far along with the pattern matching discussion and then herb sutter came out with his pattern matching paper which had a completely different syntax and our ours was based on pattern matching in other languages we were kind of building off of that kind of tech and he rethought the whole thing um so it's i mean i'm sure we'll get through it it's just it slows us down right but i mentioned that this was several years ago when this was originally proposed um so we have the latest papers um one of them is p1371 r3 um this is uh bruno sergey michael park myself dan sargensen and bjarna strauss tripp and and yeah you mentioned that bianna was there it's true but they're okay i remember going to a presentation with bjornik ah yeah sure maybe he i think he also wrote another paper which didn't include these co-authors that was kind of with some of his ideas very cool though um so the other one is pattern matching using is and as by herb so this is the other syntax and i work just kind of has paused at this point uh i i think that people are overwhelmed with a lot of stuff but also realizing like this is just it's gonna take forever and it's not even a sure bet that we'd get it in there right that's the gap okay cool let's talk about something else let's talk about reflection and injection and active use all right and we'll see what i mean with by active use in a minute here but let's talk about c plus first so there's this library called cli 11. okay it's cool library right it's decent i think i think bloomberg recommends this for modern code not that it matters to me anymore but basically you create this app object you have a stood string called file name you this is assigned to default because that'll be the default for this you call app.ad option so you have dash f dash dash file i mean you can kind of intuit what this code does it's not that complicated then you have another debug argument and then you have this macro here the third argument to add flag oh sorry sorry this is typo there yeah thank you for pointing that out um yeah no magic you know what let me tell you something this i copied and pasted from the c uh cli 11 uh like example and just just keep this in mind because we're going to talk about something later on that's going to be very appropriate to that bug so anyway they created some kind of a macro because people feel uncomfortable with try catch blocks i i don't i don't know why but but anyway you don't have to use a macro but this is their example okay so one thing i really hate about this and most of these libraries i shouldn't use the word hate one of the trade-offs that i really don't like is that const doesn't work right i really want i don't want debug to be a changeable thing right i don't want to know to have to worry that it'll be changed like somewhere lower i want to have like a lot of cons so that i can reason better about my program so that's just one of the drawbacks of this library unit testing is tough with this i don't see people unit testing their argument processing a lot because the libraries just aren't they don't really look like they're they're tough to unit test you have to add do a lot of extra work afterwards i'll explain how to do it um so the comment is talk to me afterwards i'll explain it's not a big deal then but still i don't want it all right because i'm going to present an alternative obviously right so let's look at rust all right rust has a library called struct opt so basically you create a struct okay and note that there's some like annotations above here right this is basically opting into their uh meta programming thing and it you have about equals app description they're basically adding these annotations and what it's going to do is it's going to transform this code into something else okay here we have the bug all right it's a bool and the annotation here is struct opt which is the name of the library short and long what do you mean by short and long short means i want to have a dash d long means i want to have a dash dash debug how does it know dash d and dash dash debug well it's the name of the variable right it's able to pull that out of it that's pretty sweet i don't have to repeat myself here's another one with the file argument and we have short long we also have a default value in there we have a help string all these kind of like extra annotations and here's what it looks like in main i call this function that got synthesized from this called from args and it gives me a value of this type and there isn't a mute here right so this is actually const i get the const that i want that is sweet that's what i want right i want to define the data of what i want to be the post-parsed thing and what i wanted to go into and i wanted to do all this stuff for me that i don't really care about i saw a couple hands up uh i'll start with you oh the question is is from args a member function of opt no it is not it is like a static function called someone said it's an associated method i'm trying to use the c plus lingo here so given the audience all right another comment uh also another benefit of this uh it does a lot of this work rather than constructing a parser another comment is that it does a lot of this work at compile time as opposed to constructing a parser at runtime that's cool but you know for argument parsing that probably doesn't matter as much for my use cases um we got more process launch uh the question is does from args create a parser or does it actually parse the arguments it's the latter it parses the arguments uh like it can there's like a way to get the arguments that got passed and then it uses that yeah any other comments questions i mean this looks a bit like macros i'm not a rust guy so the so the question is like a bunch of macros so the comment is doesn't this look ugly to you um it looks like a bunch of macros which we frown upon well beauty is in the eye of beholder this i i will talk about how the rust meta programming model is implemented which is actually not great but what i want is this functionality right i don't i don't care about the syntax i want the functionality okay all right um i see i see more hands like do you really want me to to do it okay all right um go ahead strictly speaking this is not the most crazy functionality you can get when we upload passing tai song has this thing that allows you to delete the entire password from docstring if you see transpose can do that as well okay there's a comment that uh there's some python library out there that can derive the entire argument parsing from a like a comment string or something like that okay but there's an argument um library that does exactly this this way okay there's a comment that there is an argument parsing that does it exactly this way i don't disbelieve that but this this is the library that we actually use in in most c plus plus code so i'm just going based on what what's actually happening i still want this um so i'm going to move forward because we got a ton of material okay um anyway yeah this declarative i don't repeat myself i can easily write tests for this code got it another thing that's kind of cool is that when i put dash dash help on this thing that got generated you see how the help string for dash file it says a help string obviously it pulled out from the thing but it also pulled the default argument right it just threw that sucker right in there now if i was doing this in cli 11 i would have to make my docs stream mention the default now that's just another thing that could get out of sync with the actual default i just think that's kind of cool all right let's talk about f strings all right c plus plus had an evolution of how we output things okay we started out with this c out i is i and j is j it's long there's a lot of syntax it kind of this is the kind of thing that gives c plus a bad reputation um it was probably a cool at the time but not anymore right so in c plus 20 we added the stood format thing so stood format returns a string and it you basically it looks kind of like print f except it has more uh more guarantees about the correctness it'll it'll catch some errors at compile time it's better right and then in c plus plus 23 um we made stood print so we don't have to do c out stood format because this is going to be really common obviously right so this is just the evolution of what happened in c plus now let's look at python python actually had a similar evolution except they started um with something a little bit well i guess they started the same kind of like line six up here you have commas it's a little bit tighter syntax but they still wanted to do something better so then in python 2.6 and about 2008 they added this the curly bracket notation and you basically have string dot format and that's where you put in your your substitutions essentially and then in python 3.6 they did something completely crazy they're like you know what i'm just going to put the expression that i had on the right hand side like right in line right they put the curly brackets and then just put the expression right there and they're like we call that f strings right that's where the f comes in okay that was just the evolution of python and you might recoil at the hideousness of this but if you've ever written a bunch of code that has to output stuff like this is nice right if you have access to this in python you use it it's cool so now let's talk about rust russ started out in you know 2018 and earlier with the python style syntax that was cool and then in in 2019 somebody's like hey this python f stream thing like that's kind of cool let me see if i can implement that and with rust meta programming and they did and so you can see that this was like a library that i pulled in where they implemented f strings that's just like really powerful meta programming um and and since like i don't know a couple weeks ago actually they just added this to the rust standard library and now it's there but look at the timetable like 2018 2019 2022 they just like boom boom boom boom added this stuff that's really quick evolution which is a nice thing okay so the question is how do you opt out of that if you really want to print the curly brackets i don't know like you could figure it out somehow okay everybody's answering the answer like it doesn't matter there's a way okay that's it let's move on to another cool uh result of having good metaprogramming facilities there's this thing called rocket okay look at this function this function you should be able to understand this function it takes in a name it takes an age the name is a string the age is a unsigned 8-bit int and it just returns a string there's no return here you can kind of like align that and rest and you put call format and basically you return the string hello 12 year old named insert x person who i want to make fun of today so then there's this piece right where they're adding this like annotation and what this is doing is it's saying go ahead and make this into a piece of an http server where if you give this url hello slash name which is a string slash age which is an age and it will just make sure that name is actually matches string and age actually matches this u8 and it will reject anything else and you just synthesize a web server with that with that one line that's pretty that's really convenient right that that i'm just thinking about like what that looks like in c plus plus and it's just like oh like this is so much but this is this is like the the essential thing that you have okay and i will say that in terms of this rocket library which is super interesting this is just scratching the surface of the the powerful library that this thing rocket is it's very cool okay so i agree with you uh looking at that slide but basically i mean okay so in simpleplus i could write this class and it gets the parameters and do whatever so this is a comment that you could write a c plus class i mean there are some of this you can do in c plus plus right you can get the types of the arguments you wouldn't be able to get the uh the identifiers um but yeah i mean you could do some stuff considering that what i'm trying to say maybe you're is something that is like so i could do it in sql space to have that to be done in one land and yeah of course there's no word behind it maybe what you're looking for is the library edition so the comment is you know maybe what you're looking for is a library edition we will get to the gap in a minute okay comment if i'm the kind of person who doesn't read documentation does theoretically yeah just let's say does this mean i have to read macro meta code in order to understand what git does so the comment is if i don't read documentation am i going to have to read the code no macro am i going to have to read macro meta code i mean i guess like yeah so that's an important comment no do you have to read templates let's keep moving on like next question for us not focus so much on the examples and get to the wider one so the comment was elmo which stands for enough let's move on i think all right what do i want out of meta programming i want the libraries that the meta programming power produces that's what i want um by the way the mechanism for this isn't great in rust like even if you look at the rust book in the in the section on on macros it tells you like hey there this isn't great um gives you some examples and they say like we're working on replacing this and the state of the art is like well actually people aren't really working on replacing it it's just kind of in a sucky place but by the way circle makes all other languages look really primitive in terms of meta programming here so i it's just huh circle let me tell you what circle is so awesome it makes circle look like crap so also by the way i really i don't care i don't care if it isn't great i just want to use the results if like a few people that are experts in this like go off and make the libraries i don't care that's fine i don't actually need to do this myself as just a few people can do it that's okay with me if it's if it's an easy to use thing that everybody loves more power to you but like for my use case i'm fine with just a few people having access to this what happens when you misuse these these libraries that's really the key thing if they manage to make that good then i don't care how old it is so the co so the comment is what happens if you misuse these libraries so i did try to do like the web server thing in rocket and i will tell you that um i wasn't like i didn't think the error messages were hideous i wasn't like oh my gosh nobody's gonna be able to use this because they are messages i thought they were good enough whatever that was the impression that i had when i walked away from it now maybe they're not great but good enough is what i walked away with um okay so what's the gap like what's the gap to getting this this greatness in c plus well there's this reflection ts that got created um by matush axel and myself and this gets us some of the way there right um so but this isn't in c plus plus there's another paper called scalable reflection by david wyatt andrew and faisal and this is basically so reflection ts was written using template meta programming style programming right it's using a well understood facility for doing meta programming that everybody hates now this is using constexpr meta programming this is this later paper here the problem with this is that well const explorer isn't really feature complete so meanings we need to add a bunch of features to the language in order to make that work so there are controversies around this there's still an ongoing controversy as to whether we want template meta programming or constant experimental programming for reflection like again my position is i don't really care if it's a bit ugly i just want the feature so i would be completely happy if we just took the reflection ts and like threw that in the standard and continued working on making something better but i think i'm definitely in the minority there there's also a big controversy about whether we want a type rich or a monotype kind of library so type rich means that if you reflect upon a type then you get a meta type if you reflect on a value you get a meta value a monotype approach is more you reflect on a type you get a reflected thing you reflect on a value you get a reflected thing it's like all the same type so it's an api design question and there's ongoing controversy but this has been paused i just haven't continued working on this paper there was like a compromise paper and and that was it and then there was circle meta programming which got completely and utterly rejected we should talk a little bit more about that so circle style meta programming this is the clear winner in terms of meta programming why why why is this a why was this a problem people mention security as the primary concern they're concerned about security because if you compile with something that powerful you might end up compiling you might be running third party code at compile time right it's it can be scary if you've never had that before but it's not an issue for rust or python or like eight million other things but whatever for c plus plus apparently is a problem the reality here is a big paradigm shift needs to overcome serious inertia right and c plus has a lot of inertia now if now we could continue fighting the battle and say like hey we want something like circle meta programming so that we can have this power we can try to push that and get that turned over but given the state where it is now where it's like completely and utterly rejected and where we need to be to where we have consensus that could take years it's like a lot of effort a lot of papers and all that kind of stuff and there's no guarantee of success so who wants to get who wants to take up that battle sean do you want to take up that i know i saw how well worked for you okay so it's it's it's an uphill battle i i don't know how that can happen so i talk a little bit about this in my don't concepts where all the things talk including a completely uh video of what happened in the committee it's fun if you haven't seen it check it out is there a build system that does not involve running arbitrary code at compile time there are but they're but they're they're limited right um the comment is there are and there's limited i don't want to go down that rabbit hole like we can later but you get the idea like it for sure all right there's a bunch of tools for systems engineers in here some of these are big things some of them are small things um there's the memory safety features right if you have a mutable thing and you take two different references to it and you try to up the two different references and rust you're going to get a compiler error it's like look you can't have two different references to something there's a lot of stuff in there to prevent certain classes of errors i'm going to say this is a nice to have it's not that big of a deal where am i making this like extremely bold uh statement since everybody a lot of people say like this is rus key feature i just haven't seen a lot of production issues due to bugs like this and i've sat in a lot of postmortems and i love postmortems i just haven't seen a lot of production issues that stem from these kind of memory issues um i think that is because it's largely mitigated in c-plus plus with other things like sanitizers asan ubsan this covers some of these cases training helps out best practices people are using smart pointers uh they're using abstractions there's a comment that i didn't go to dave's keynote yeah sorry about that i i was busy so all right so so so here's another cool thing it's called unimplemented it's pretty basic we have a struct point all right basically so this is where you write your member functions in rust just ignore that part but basically i have a function that i have declared here called origin it returns a point in the body i just threw this unimplemented thing and that compiles just fine like here's a function called foo where i match different things and part of it i just threw this unimplemented piece here um why why why do i want this well if you're doing top-down interface style development it's really nice to be able to start with like hey i'm going to write the example this is what i want the top level code to be and be able to put unimplemented for the various pieces so i like that um it also allows for better compiler and editor interaction like i know there are people who are like i get so upset when i see the squigglies under my you know my words when i'm writing my email okay that's cool like i don't get upset i actually like that so i can right click and fix it real quick and a lot of people like that in their ides in terms of their programming they want to see the squigglies when something is an error so that they can kind of look at something which is going to compile so that helps with that what's the gap for c plus i don't know i mean you could use something like decalval for this if you wanted to but then you have to specify the type which isn't great maybe some kind of compiler intrinsic more powerful meta programming could you do this i i don't know i mean basically just someone needs to write a paper and investigate it and the comment was uh we need the bottom type yes this is exactly what this is the bottom type so the question was how is this different from built in unreachable and the answer was because it can return a value of any kind of type it kind of deduces the type piece okay um another thing i think that is cool is side by side tests so here we have a function foo that just adds 41 to a and here we have the test for it so what um but the thing is this is in the same file right the i have the function and then i have the test in the same file there's a comment my comment was i have seen at least one c plus plus library that has done this i have two and uh we'll talk about the gap so why do i want this well it's generally agreed upon by everybody except for uh for sean my new boss that documentation should be close to the code right people like this um the same is true for tests this the same rationale is true for tests um you can it'll be accurate up to date less likely to be missed um what's the gap basically a proposal someone needs to do this um you could do this with static initialization hacks i've seen libraries like this so so you can do that but that's like there's a lot of terribleness in that um do we want to encode in the standard different compilation modes one for your test one not for the test uh that means we'd have to have agreement between different build systems i i don't know it's it it's tough it's tough it's maybe possible um dot comments when you're writing large scale systems you care about comments so here's our function foo again and then here is the the dot comment okay so what it's markdown syntax who cares look at the code in there what does russ do for me so what i mean i can do this in c plus plus rustdoc will verify that the code in your example compiles that is sweet it will verify that the code actually works it will run the code and make sure that your examples like with this assert here are actually do what they say they're going to do that is pretty freaking sweet right and remember when i was talking about that thing i copy pasted from cli11 documentation if they had this there wouldn't have been a bug in my slides okay why do we want this well keeping examples correct is expensive uh you have to like be very careful about running the code and then copying and pasting it to update it most of the time um what really is just keeping examples correct is just unlikely because it's so much work and what's the gap well we have doxygen which is a popular the most popular system and doxy press is now gaining more popularity we need this additional feature in one of these systems well build system issues are going to be a major complexity here because like how does the documentation system going to interact with the build system to be able to check this code and we don't really have an organization to drive this kind of change right wg21 the standardization body they drive the language right they wouldn't drive a feature in tooling as much or i don't think it has and i don't think it has appetite for it anyway um so that's what the gap is uh the comment is that the sg15 chair is willing to get papers on this great and then then we just need to get consensus on those papers yes uh the former bryce it's not going to go anywhere the former sg15 chair says yeah but it's not going to go anywhere sg15 is the tooling study group of the c committee sg15 is the tooling study group of the standardization committee thank you for that clarification for those in the audience that didn't we're not aware of that okay cool let's move on to something else let's talk about concurrency okay i am not an expert in concurrency uh so the the thing that i want you to take away from this code we're gonna work work through it is how simple it is for somebody to be able to program with it okay so there's this thing called channel in in rust and here we create what's called a bounded channel and you have an s and an r the s is the sender and r is the receiver okay and bounded five that means that there can be no more than five things in this queue at a time right and you can have a bunch of different threads throwing things into the sender and a bunch of different threads pulling things out of the receiver and the way that you do this is you you basically clone the sender and then move it into your thread and then the thread will use that to send things and then here i have just two different threads doing that and then the receiver you can clone that too but we're not doing that here you call assert well here we just call r.receive and then you get the value out of it okay synchronize between threads i'm sorry i keep seeing this double bar syntax i've seen it in other rust examples i don't get it isn't is that moving s2 somehow how is it referring to s2 okay so the comment is this move thing like is tripping me up like this tripped me up to this is it's basically saying so this is creating an um an anonymous function and it's saying to have the captures come in by move that's what that's doing yeah yeah sorry it's it's not obvious all right so the standard library actually has one of these this this i use this crossbeam library but the standard library has one which is slightly simpler simpler called mpsc i think that's multi-producer single consumer whereas this one is multi-producer multi-consumer but at any rate it's there it's accessible it's really easy to use okay and and they use it and this eliminates a bunch of common error classes when people are trying to do mutexes and stuff like that for synchronization so why do i want this i want it because run away run from con run away engineers run away from concurrency in c plus and they should right because they're going to screw it up um and then when they do do concurrency in c plus plus they reach for the error prone tools that come with the standard library they reach for mutex they reach for studs thread they don't use like higher level abstractions and it's it's just a disaster so that's why i'd like to have something nice that they can use that isn't a foot gun what's the gap well we do a lot of bottom-up design in c plus plus as opposed to top-down design like and when you do a bottom-up design you typically don't get things that are very user-friendly or ergonomic and it's just we do this a lot in c plus so we need to kind of like change the way that we think about things and maybe do some things that are top down there's also big disagreements about standard library scope some people think that the standard library should only be like the absolute lowest level of things the the bits and the twiddlies okay we'd have to get over those disagreements if we were going to get something like this in the standard library and then there's also a big focus on completeness and generality that this can be used everywhere versus hey this is going to be the ergonomic solution for 95 of our users right it's just we need to like work through these things all right now we're going to talk about community all right this is this is this is a big one we're going to talk about inclusivity all right the rust logo is or their single sentence thing is that rust a language empowering everyone to build reliable and efficient software and they put extra emphasis on empowering everyone it's funny when they talk about this they're like yeah there's that other stuff but like this empowering everyone is really a key thing okay now when i say inclusivity i know that a bunch of you got like something in your throat and it's just like oh i just want to clarify what i mean when i say inclusivity okay when i say inclusivity it is not silency silencing oppressing or removing folks that disagree with an ideology it's not accepting one group by rejecting another it's not mob rule this is not what i'm talking about inclusivity is a mindset it's thinking about how can i reach out to these people that are currently not in my group all of you here you're in okay and then those who are not here are not it's thinking about how do i how do i close that gap how do i get them to come in the ones who aren't part of this that's what it is it's about bringing people together it's about doing reasonable accommodation you know what can we do to help people come in in a reasonable way it's about accepting people for who they are it's about extending our reach this is what i'm talking about when i use the term inclusivity okay all right now let's talk about inclusivity and rust what does it mean in the rendering of rust since it's such a big thing so this was the theme of ruscom 2020. i highly highly recommend uh if you have any interest in this to watch the opening keynote of ruskoff 2020 it was fantastic you don't have to know russ to watch it it talks about how they built their inclusive community and what the impact of that was one of the focuses that they have this eye here you can't tell that is bolded but it is bolded they want people to walk away from a talk saying i can do that right now how many talks do we have in our community where someone comes up and does a triple backflip and in the audience you look at that and you're like well that was cool i'm not gonna do that right this is what they want people to walk away from with when they hear their talks okay and it's the focus of the community they want a community which is accessible to beginners they don't want to have a place where if you're a beginner you're going to feel out of place you're going to be like ah well this is clearly for experts i don't belong here they want the community to be accessible to beginners and they want rust accessible to beginners too not just the community but the actual language and all the tooling and everything around it so they had this big focus on error messages for like a few years where they looked at every single error message that came out of the compiler and they said how do we make this error message absolutely awesome and if you get an error message in rust i can tell you it is so cool because it'll say like hey here's the error message this is probably what where you made your mistake and here's a link to a documentation of the common ways that people can get to this error message and this should help you out that's so nice it's because they're focusing on it like it's we could do that but they're focusing on it okay their code of conduct is cool and one of the things that they have in their code of conduct which i haven't heard anywhere else is they talk about accepting trade-offs like if you see an idea which you think is a bad idea frequently you'll say like you know i think this is a bad idea i think we should go with this they say hey here's a little tip how to make an inclusive community instead of saying hey i think that that's a bad idea here's a good idea they'll say that idea has a certain set of trade-offs here's what those trade-offs are i have this alternative idea and this has this different set of trade-offs now let's talk about like the difference of the trade-offs you see now it just became it went from a combative discussion into a mutual discovery of truth i think that's really sweet um they also prioritize spreading knowledge they really care about making sure that we don't have like a small group of people that are in the know and then a bunch of people that aren't in the know it's like hey if there's something that people need to know let's make sure that we give talks and we can spread this knowledge out they do that um they also care a lot about the accessibility of their tooling they say you know they want to use github versus emailing patches around because it's so much easier to get into github and get a conversation there they want to use discourse as opposed to mailing lists they want to have use discord as opposed to irc these are intentional decisions because they would like to make their community inclusive i think that's cool mentoring and community building are also considered absolutely essential as part of the discussion that they have so what's the gap in c plus well i mean we probably need some organizational help right to to carry a vision out like this there's the c plus alliance the c plus foundation the boost foundation these are possibilities also include c plus plus this is kind of their mo um but they need to decide that this is what they want to do and also like what is the strategy you know is it like cooperation or is it making demands like i've heard many times like i will not go to your conference unless you do this or that or this kind of thing what kind of environment does that create is it an environment you want to be part of an inclusive environment i don't know i don't i don't like that environment personally how do we channel energy productive productively you know someone gets really upset about a particular thing how do we channel that into a productive channel as opposed to the destructive channel like we got to think about this um do we got to decide do we want to change existing structures like say like throw out iso c plus plus and like replace it with something else or do we want to work with existing structures to try to improve them okay like we got to decide what we want to do with our strategy here and then also we have to decide what is the goal we can't focus on every single community at the same time right we won't be able to do very much um what what russ did is they said hey the community that we have missing right now that we want to focus on are beginners and people who aren't traditional systems programmers like people who are using other languages like you know javascript or something and tell them like hey you can be a system programmer too so we'd have to decide what we want to focus on it could be women it could there's like so many different things we could do but we got to focus so that's all i'm going to say about inclusivity but one of the things that came out of it is this ergonomics initiative in rust and i love this quote from the rustcomp 2020 keynote i'll say it again please watch that they said a paper cut for an experienced user can be a blocker for a new user it's so true there's something important to think about they think about ergonomics they think about it through everything so remember that example that i had that you know struck opt thing i just tried it i just put like dash dash dev that's not that's not correct here's what the output was found argument dash dash deb which wasn't expected or isn't valid did you mean dash dash debug and then it gives the documentation for dash dash debug right below it why did they do that they did that because they keep on thinking about ergonomics all the way through so even if you like just any rust developer your programs are automatically better because you use the library which is ergonomic now your command line arguments uh thing is better right i think that's pretty sweet another thing like their package management thing this is also part of their ergonomics so i made this presentation using org mode and in org mode yes it is the best thing ever in org mode i have this inline code block right and look at that first line up there i have you know hey this is a rust code block and by the way i have these flags and it depends on struct opt and what happened when i hit comma comma on this thing and it compiled it it and ran it it went and it fetched that package which this is this is dependency of the snippet and it used that to compile this and then it ran it and that was it even that rocket example rocket you know i think this is like big web framework dash dash depth pulled it in no problem like that is ergonomic right so i thought that was pretty cool another package ergonomics thing their hello world looks like this you put cargo new hello rust this is basically creating a new package you go into it you say cargo run it compiles it and it outputs hello world what happened there when you create a new package it created a main.rs with hello world like how many times do you start an effort with a hello world example right they don't make it so you don't even have to type that in it's like there it's fantastic so what do i want i want to have the word ergonomics used as frequently as i hear the phrase zero cost abstractions and discussions right it's just not a word that we use that much that's not a thing that we discuss very much but we could start doing that i would like to see a c plus plus ergonomics initiative right i'd like to see a wg-21 ergonomics working group to consider things from an ergonomics perspective because the other working groups they really don't think about this that much so what's the gap leadership who would be willing to lead an ergonomics effort in c plus one awesome two awesome anybody else okay the person behind you had their hand up i know that this does not mean end up for marshall okay can you guys talk with me afterwards if there's leadership who would be willing to volunteer their time and effort into helping make seepless plus more ergonomic one two three four five six seven eight nine awesome we need to like please come talk to me afterwards let's get all these names together we need sponsorship and organization for this right could boost be that maybe this is this is something doable so comments almost everything sg15 works on is so the comment is that almost everything the sg15 works on is in the ergonomics space cool comment that whenever that is made entirely out of papercuts there's very little appetite amongst the sort of you know language lawyer people that are in ewg for this kind of thing like this language for so the comment was that when the stuff is brought up in the standardization group a lot of there's a big response of like yeah but c plus plus is complex like we don't care about that we're experts um that's very non-inclusive thing it just is uh bryce all right bryce said something controversial and moving on how much of this gap at least on the language side comes from good generative meta programming and able the ability to use macros to make things smaller like that so the question was how much of the gap is due to not having a good powerful generic amount of programming stuff i think it depends on the piece like some of these pieces need that some of these pieces don't need that i actually i'd like to kind of answer that question i think i think that you know that that is a that is a downstream symptom whereas the inclusivity thing is the upstream driver for all of that so that's where you get ergonomics right if you don't care about inclusivity for beginners and whatever then you're not going to care about uh so the comment from dave is that basically if you get your inclusivity right that drives all the other things that that's that's really the key piece there and i totally i i just agree to say that that's a great presentation i really agree with a lot of the things that we say i i would hope to i would hope to achieve some of those that will be able as a community to and just the more technical thing just the common thing i think that i've noticed in our examples and probably notice was that basically this is like writing english telling the compiler what you want to get just like with the server and that's yeah that could help us awesome how am i going to repeat that i'll just say the the first thing that you said this this is a great presentation thank you all right um so looking ahead there's there's a lot here that's doable there really is um medium-sized language features we could do that uh community building cultural shifts these are things that we can we can do like this is totally doable we will do nice tooling we can do tooling but there's some bad news too there's a lot in here which is really hard slash impossible substantial language features i i don't know how to do this effectively uh in c plus plus our costs are like 10 times higher than like the other languages in the space our latency is like five times higher it takes us a long time to get things and our cost is just huge to like push things through uh we have a lot of baggage in c plus plus a lot of baggage we've got backwards compatibility baggage we've got big inertia problems just where we've been doing things this way for a really long time and it's just it's really hard to change that so i have a few questions for you so first question is do you think that in c is c plus plus do you think that is under threat of becoming a legacy language defined legacy like kind of like a legacy system you something you wouldn't use for new stuff yes okay i hear a lot of yeses okay here's and the comment was less so than it was in 2008. okay cool now is it inevitable that c plus plus will become a legacy language is this yes okay i hear a couple no's i hear a lot of yeses okay there's a comment i i mean it's you have to decide what you mean when you say c plus right down the road like c plus as it is written today i do believe that will be a legacy language but it's always possible that we find a way to build a new language inside of c plus and that is c plus and you know it's got some kind of backward compatibility mechanism so uh the comment was and and it's absolutely true is that a new language could sprout out of c plus that is c plus plus and is compatible with c plus but like the old c plus plus style would be like a legacy thing yep um i think i saw your hand up first i think a more immediate threat is that it becomes a niche language uh the comment was there's a more immediate threat that it becomes a niche language and i saw hands over here um my comment was just drawing a distinction between a language that you maintain for existing for making new software where you don't have existing software assets versus making software where c plus plus core value add is your ability to reuse existing software yeah uh i don't know how to repeat that but i agree um okay i can think of few things that sound more depressing than the idea that one language that exists today would be in use as an active language for the entire length of the universe all right so i'll repeat that um lisa lippincott said i can think of few things more depressing than the idea that a language which is in use today will be in use for the entire length of the duration of the universe um so i'm going to ask one other question is c plus already a legacy language okay a lot of discussion that's great it's time for the discussion all right so so this is the end of my talk we'll go into comments now [Applause] you missed one of my favorite rust patterns which was typestate which utilizes the same memory mechanisms in order to get constraints on states in your program encoded into types yeah so comment was uh you missed my favorite rust pattern which is uh basically linear types like like type based constraints um yep i'm sure that i i missed a whole ton of people's favorite rust features so fair enough so i think the common criticism that was kind of raised by a few people implicitly of the macros that a lot of the things you showed depended on um is that there don't work in large good basis potentially and there's not enough evidence of that in rest or whatever but we have that in python we have that in languages like java and dart what is the status of banning reflection or allowing reflection in large code bases in other languages uh the question is what is the status of banning reflection in large code bases in other languages i haven't heard of such a thing but i'm not super knowledgeable about it so i don't know now so um i as much as i want the audience which isn't here to hear all the comments i think i'm just going to switch to let's just have a discussion um so i don't have to repeat everything all right so uh goodbye audience on youtube you
Info
Channel: CppNow
Views: 36,732
Rating: undefined out of 5
Keywords: Rust Features, boostcon, cpp, c++, programming, coding, cppnow talk, cpp talk, coding bootcamp, programming tutorial, c++ programming, c++ tutorial, c++ programming tutorial, cpp programming, boost cpp, boost, rust, features, aspects of Rust, systems programming language, Rust programming language, features of Rust to add to c++, tooling, community, strategy, advanced C++ topics, backwards compatibility, David Sankel, CppNow 2022, david sankel, the ISO process, cppnow
Id: cWSh4ZxAr7E
Channel Id: undefined
Length: 74min 39sec (4479 seconds)
Published: Sun Jul 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.