Understanding differences in syntax and language structure.
Exploring Rust's ownership and borrowing model.
Comparing memory management approaches.
Understanding type systems and error handling.
Exploring concurrency and performance differences.
Translating familiar Java concepts into Rust.
Understanding when to choose Rust or Java for software projects. In this episode, we continue our exploration of Rust for Java developers, comparing the two programming languages through practical examples and side-by-side implementation using Java 17.Moving from Java to Rust introduces a different approach to memory management, type safety, concurrency, and application development. This session examines how familiar Java programming concepts translate into Rust and highlights the differences developers need to understand when transitioning between the two ecosystems. By comparing code examples and programming approaches, we explore how Rust achieves memory safety and performance without relying on a traditional garbage collector. Whether you are an experienced Java developer considering Rust or a software engineer looking to expand your programming expertise, this episode provides valuable insights into both languages. What you'll learn:
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
Speaker 1Rust for Java Developer Part 2. In this episode, we're going to talk about array, topo, vector, and enum. And then we'll have an integration to generics, and we'll talk about two enums, option and result, that are very commonly used in Rust. Okay, so let's get started. In Java, arrays are fixed size. So the way you define an array with a type, the bracket, the name of the variable, and then you use braces to initialize it. The way you access a member, is with the bracket here. So
Speaker 1we're going to access the last one. In Rust, the arrays are also at fixed types, so the way you define it is with let, the name of the variable, and the way you initialize it is with bracket. And the way you access a member is the same, with array, one, and here we're going to access the last one. Press save, and we have exactly the same. In Rust here, by default, the type isn't fair to be I32, and then we have our four item. So if we want here, we
Speaker 1can make it explicit. So if we want the number to be U64, for example, and then semicolon, and the number of item we have in the array. Press save and everything works fine. Now, obviously, if I put five, that would be an error because the initialization doesn't have five element. So in Java, if we want to initialize a blank array, we do with new, the type, and then the number of item we want. So here, 10, for example. And then by default, here, the value will be set to zero. In Rust, you can also blanket
Speaker 1initialize an array, but you have to provide the value. So the way you do that is you provide that value, for example, 99, semicolon, and then the number of item that has to match here. Press save, and then all of the item of the arrays has 99. So obviously here we have a primitive type, but we could have a class. And if we set that as a class here, for example, an array of foo, press save, and now we display new. In Rust, we could have the same thing here. So for example, if we have a struct foo,
Speaker 1we can have an array of foo. But now to initialize it, since Rust doesn't have new, we need to initialize with a value. And the way it works here is we construct a foo, but the foo has to be clonable. And the way we add kind of blanket implementation here, we can use a derived macro to implement clone and copy. And then we're going to add debug to be able to print it. That is a way that you can easily add implementation to struct. And those are the common ones. So then after here, we're going
Speaker 1to have this notation here on the braces to be able to print the debug information, print save. And now we have the item foo. So all of the four items of the arrays will have a different instance of foo. So here in both languages, an array is fixed size and same type. So in Rust, arrays belong to the primitive type category, which is called compound types. And there is another type, which is also fixed types and can have multiple items. And that is topo. So in Java,
Speaker 1you don't really have tuple as part of the language. You can use a library here, which is Java tuples that provide classes like pair, triplet, and so on. In Rust, to initialize a tuple, you use the parentheses notation, and then you put your value. So here, for example, one, two, three, we can put a Boolean, even a struct here, an object. So we're going to construct a new foo. And the way you access a member of a tuple is with a dot notation and then the index, like an array, but the dot notation. So here we are going to
Speaker 1pick up the second values, the true, so it would be index number one, press save, and then everything works. In Rust, you can create, you can return tuples anywhere you want, and it's actually very useful when you use it with match and other Rust construct. And a tuple is unnamed. However, you can name a tuple if you want, and that is with the word struct, it's called a tupper struct so here we can have bar and we're going to have bars that is going to take a u64 for example a boolean and a food data type
Speaker 1and now the way you will build this will do a bar press save and in this case it works exactly the same because that was the same type so without the bar here that doesn't have to do anything with a bar and the infer type is i32 bull and foo But if we put a bar here, now that will be the type bar, like a struct. And we have U64, bool, and foo. And obviously here, we could have done unname as well. So we could have done the I32, bool, and foo. Okay, so now let's
Speaker 1go to vector. And a vector in Rust is like a dynamic list, at least that can grow. And in Java here, that is called the ArrayList. So in Java, the way you define it, you have to import it, and then you can have ArrayList and Tigger, and the way you access a member, array to not get zero. And we get item one, two, three. So in Rust here, the way you define a vector, we're going to make it mutable, and then the variable name, and then the type here, which is going to be Vect. And so that is a type which is in the prelude, meaning
Speaker 1that you don't need to import it. And then here we're going to see a first little peek at generic. So here we're going to say that the type here, for example, is U64. And then we're going to assign it a new empty vector, Vect. new, which is a static method. And now we can do array to push and then one, two, three. So the ROS compiler will have one, two, three as a U64. And then you have two ways of accessing the value is one by
Speaker 1the array index. So if we do array two, and then we do the zero here, press save, and that will display one, two, three. And then the problem of this one is that if you go outside of the array, it will actually crash. So the other way here to do it is usually you iterate through an array such as you don't have this issue. Otherwise you can do a get and then the index. And now if we put a debug here, because that will return an option, we'll talk about it later. And now you get an
Speaker 1option as a value. So here I do a get zero that will do sum of one, two, three, meaning there is a value inside the option. But if I go to outside of the index out of bound, press save, then I have a none. So nothing crash, I can handle those case without crushing. So in Java obviously we can have any type here, so we can have an array of foo, and then here we're going to add new foo, create a new instance of the foo object, press save, and then we get the foo reference here.
Speaker 1In Rust, same thing, so here we can do a vector of foo, then here we'll create the foo instance of our struct, And then here we're going to display the option of full. Press save. And then we have some full. So obviously here, if we had bracket, then we'll get the full. So in Java here, when you're out of bound or you get an exception, and then Rust, if you don't want to crash when you're out of bound,
Speaker 1you use a get. Okay, so now let's talk about the enum. So in Java, the way you define an enum is with the enum keyword, the type name, and then the variant. Slipping, skiing, coding. The way you instantiate and assign an enum is with the type name dot the variant name. And then the way you branch over the variant of the enum is with switch. And here I'm using the new switch instruction here, which could return a value. And then we have the case slipping, skiing,
Speaker 1and coding. and you can have fixed value for each variant and some method for the enum activity. But in short, here you have three variants and they are completely fixed from a data standpoint. So here, if we press save, we get hopefully in Java. In Rust, at first glance, it's really similar. So first is the enum keyword, the type name, and then the variant. The way you instantiate a value and assign it is with the type name, colon, colon,
Speaker 1and then the variant name. The way you switch is by using the match keywords, which is kind of a super switch, and we'll see that later, the variable name. And then after we have our three variants, you put the type name, colon, colon, and then the variant name. And then after you have the arrow, and then the expression here that you have. Press save, and we have almost exactly the same. But the true power of enum is that enum can have data, any custom data you want.
Speaker 1And each variant can have different data. So for example, sleeping here, we can say that we want to have U8, which will be the number of hours slept. The skiing, we can have the ski resort here. And then we're going to say that coding, we don't need any specific data. And so now here, what we have is we can have the sleeping and then here we can have the hours. And so the data that will be attached to this variant, we can have it at runtime. And so here we can do wake up and then the number of hours.
Speaker 1And here for the skiing here, we're going to have the result variable that we can display here. Now, if we press save now, works exactly as before, because in fact, the coding didn't take any values. So nothing has to be changed. But now if we do the variant skiing, we have to give a string. So we're going to give the result names. Press save, and now we have awesome palisades tarot. And now if we do the sleeping, if we save it as such, we get the error
Speaker 1because it doesn't make any sense. The sleeping variant doesn't take a string, it takes a number. So we're going to say nine, press save, and then that's it. And now it even gets more powerful than that. So first here, any match here needs to be exhaustive, such as if you only have two, press save and you get an error. because it says you have to match everything. So if you don't really care about the sleeping, you do the underscore, which is going to be the catch all. And then for example, we're going to return nothing, void. Press save,
Speaker 1and that works exactly the same. Now if we go to our previous example here, it gets even more powerful than that. Let's say that we want to wake up only after eight hours of sleep. So here we can say if hours greater than eight. And now we need to make sure here that we're exhaustive And we're not missing a case here. So what we're going to do here is for the rest here, so without the condition, which will be the catch all, we're going to be quiet. Press save. Now we have to wake up. And now
Speaker 1we can sleep. So here we're not using the arrows. So the idiomatic way when you don't use a variable, it's either start with underscore, or in this case, we just put underscore. Press save, and everything works exactly the same. And as we have seen with if-else, here with match, all of these arms are actually expression, like the new Java 12. But now you can have complex rules here at the arm level, at the condition level, and then you return the expression. So for example, here, if we didn't want to println, we could assign it to a variable, message,
Speaker 1and at the end here, we do a semicolon because an assignment is always a statement, but each arm is an expression. And so now, for example, we can replace all the println by format, which works exactly the same as println, but return a string. And now we can have one println for everybody. Press save. And then we have exactly the same thing. And if we sleep too much, we have to wake up. In Java 17, the switch
Speaker 1became a super switch as well. So in fact, we can go very, very close to what we have on the left. but not with enum, because enum cannot have application data, but with classes. So rather to have an enum activity, we're going to have a base class activity. And then we're going to have our three subclasses, sleeping, that will take the hours, skiing, that will take the resort,
Speaker 1and coding, that will take no data. Now we go back up here So now for this example, the activity now will be the best class activity. And we're just going to create a new sleeping activity of nine hours. And now the trick in Java 17 is your case here can actually do an instance of and put it in the variable. And so the way you do that here is simply by having the variables that you want to have. And that basically will be the same here
Speaker 1as activity now, but casted into sleeping. And that will be put into the SL. And now that is a normal sleeping object. And we have access to our hours. We can do the same thing for skiing. And now we have our resort. And we can do the same thing for coding. And we don't need any value here. And now we don't forget to put the default. Because this is not an enum. So we could have many other classes here of activities. So we need to handle that.
Speaker 1press save, and then that's it. And in Java here, when you use this notation for switch, it become expressions as well. So what you could do here is actually put it in a variable here, message. So we're going to do exactly like Rust, and then we're going to change all of the println to return the value and semicolon, semicolon matters after all, and then we're going to print.
Speaker 1press save and then we have the same thing and actually in java it gets even cooler than that with java 17 you can actually have condition as well so very similar that what we have here so we can do here we can do an and and then we do the sl hours greater than eight and so if we press save here we have the wake up and we only sleep five hours we press here and we get the unknown because it doesn't match this one. So
Speaker 1here what we can do is do a catch all for the sleeping case, similar to what we have done here on the rust side. So you do a case sleeping that, press save, and now we have the chute. The match keyword in the rust side is a little bit more powerful because it allows you to unpack enum structure as well as work with tuples and many other things. However, the new addition here in Java is actually really great and can really streamline a lot of code on the
Speaker 1Java side. Okay, so let's talk about generics here. So in Java, the way you create generic, it was class, container, and for example, here's a generic type T. And then you have a member here, for example, an item of type T, and then you can have the constructor detect T as an argument, and then we set the item. The way you create the object is with var, for example, c1.
Speaker 1And here you give your type, and 123. And you can create another one here, for example, a second container of another type. For example, in this case, it would be a string, hello. Press save. Everything compiles. In Rust, it's very similar. The way you create a generic type is with a bracket as well. So for example, here, we create our container, type t. And then we have item of T. And then the way we instantiate it
Speaker 1is C1, and we're going to have a container item, one, two, three. And if we want to have a container of string, C2, container, item, hello, toString. Save, and everything compiles. So in Rust, when we do that, the compiler actually creates a container for each type. And it's called monomorphization. Which means that behind the scene, it's
Speaker 1like if it created a struct for container I32, for example. It's the item of I32, hard-coded. And then it's like if it created another one, container string, for example, and then item string. Obviously, it has its own way of creating that on the backend, and we don't see that. It's completely transparent. But every time we use a new type with this struct here, the compiler will create the
Speaker 1appropriate type struct. So it won't create for every types, but every time we use a type, it will notice that there's not one already created, and it will create one dynamically. And obviously, it's completely transparent to us. So from our point of view, it's the same struct. In Java, we can bound the generic. So for example, here, we can say that the type T will extends comparable. And that will allow us to write a method like a compareItem. And then we can use a comparable method. And the
Speaker 1way we call this method here is to compare it with the same type. And so for example, here, we can compare the string as well, the second one, press save, and then we get one here, which is c1 is greater than one, and then the other one is smaller. And then obviously here, if we have the bigger number, we get the minus one. On the rest side, we can have a similar behavior here, we need to import two component, one is a trait order, which is like
Speaker 1the comparable, and then ordering, which is an enum, which is a result of the order trait. So for example, here, we're going to bound the type T to a type that implement the trait ORD. We're going to implement our generic type, which must implement ORD, container of T. And we can implement our function here, which is compare item self, because it's a method. And then we have the other item,
Speaker 1which take a reference of T. And we'll talk about reference later. And that will return an enum, which is the ordering, which is what we imported above. here we're just going to do a self item cmp which is a method of order and then the other item and we don't put semicolon we don't do return so that would be returning this value here on the method press save everything compiles and now we can do the println here compare one we're going to text the debug because
Speaker 1we're going to return an enum and we're going to do a c1 compare item, we're calling our method. And we're going to do a reference of one, for example, there has to be a reference here, because we have a reference type here. And we'll talk about reference later. And now we can do the same thing with the string. Compare to the debug, c2, compare item, and that would be a reference of a string, but a full string object here, not a static string. We'll talk about that later.
Speaker 1Press save, and we have the same result then over there. And obviously here, if we put 222, press save, and our number is less. So in Java, to add generic to a function, we can do our type T, and we're going to extend the comparable. And then we can use T for the two arguments. And then we just return the value like that. And then we can add our compare like this. That will just do the function call here, the static function. And then the 222, 111.
Speaker 1Press save. And everything works. In Rust, very similar. Define the function. Compare items. And you have your type. You put your bound. And then you have your arguments with a reference of t. And we'll talk about reference later. Return the ordering. And very similar here. We do item1, cmp, item2. No semi-colon to return the value. And now we're just going to call the function here with a reference of 222 and 111. press save and everything works great. So
Speaker 1it's very similar here. The same monomorphization happened here with a function. So the function here pass I32 by default numbers are I32. And so the compiler behind the scene will create a function compare item that hex I32 as type T. And if we were going to call the function with a dot type, then the compiler will also create function for this concrete types. So that is a way that generics works in Rust. There's a dynamic way as well to work with Rust, and we're not going
Speaker 1to talk about it today, but it allows you to create a list of objects by their interfaces, their trait, rather than their concrete type. Okay, so let's talk about option and result now. So in Java, you can have new. So for example, here, if we have a class, foo, we can have a variable of type foo, and initialize it as new. And then we can print it like that. And then we get the value foo. Obviously here, if
Speaker 1we create a new foo, we'll get foo. In Rust, we don't have new. So if we have a struct foo, for example, and we have a variable foo of type foo, we cannot have new. That wouldn't compile. So one way to solve this issue is with enum and generic, like we have seen. So we can do enum, new label of t, and that will have one variant, which is new,
Speaker 1and then the other one, which will be the value that will take t. And now, rather to have foo, we have new label of foo, and now we can have new label, new. press save, and now it compiles. Now we're going to add the derive here for debug, such as we can print it with a println. And so now here we can do a println foo, the
Speaker 1braces with a debug, and then foo. Press save, and we have foo new. And obviously if we want to have a value for it, we're going to do a value, foo, and we build a new foo inside it. Press save. And then we have foo, value of foo. And Rust comes with an enum that does exactly that. And it's called option. So we don't need the new label. We just have option here. And the two variants are option sum.
Speaker 1Say that there's a value in it. And for something that is empty, is none. And so that is part of the prelude. So we don't have to import it here. And in fact, the variants are part of the prelude. So it's like if we had a use option none, use option sum. And so now we don't have to
Speaker 1do the option here. We can just do none like this. And some like this. And that works exactly the same. And again, these two things are part of the prelude. So you don't actually need to import them. Press save. And everything works very nicely. In Java, you have a similar class, which is optional, is with generic as well. So here we do an optional foo. And here we're calling the optional of and the new foo. Press save.
Speaker 1And now we have the optional of foo. And then if we have nothing, and then you have method like map and so on, such as you can chain your things. And the similar here under the foo and a lot of other methods here that you can use. Okay, let's talk about result and exception now. So on the Java side here, if we have a method like this, that just do a division and return the int of the division,
Speaker 1then we can use that like that. So we divide 10 by four and we print the result here. Now, if we divide by zero, press save, then we get this big number here, which is basically the infinite of this type int. But let's say that actually we want to throw an exception when it's divided by zero. So the way you would do that is just do an if by echo equals zero. And for now here to make it simple, we're going to just throw a runtime exception.
Speaker 1Press save. And now the program crash. And if you want to catch it like that, press save. And now the error is handled. On the rest side here, very similar. So we can have a function that divides the numbers and do the flow of it. So in Rust, primitive types can have methods, so we can implement trait on primitive types. So that is what we're using here, casting to i32. Then we're going to call the function to
Speaker 1divide 10 by 4, and then we're going to print the result here. Press save, and we get R2. Now if we give zero, press save, and then we get the same result that we got with Java. when we didn't have the exception. Kind of quick note here, we have the dot notation here at the end, which is exactly the same as if we'd done 10.0 or 0.0. Now, one way to handle this case here in Rust would be with a panic. Wouldn't be a good way, but
Speaker 1it's possible. So we could do an if by equal or equal zero. Gonna have this notation again. And then we can do a panic. And that works, but that basically crashed the program completely. So in Rust, you don't manage your exception with panic because you cannot recover from panic. It's not like a runtime exception here where actually you can recover from it because you can catch it and then manage it. But here, panic just stops the program
Speaker 1or at least the thread. And then there's no other way here to have a throw here. So in Rust, there's no throw something. So you cannot break a function execution. The only thing you can is return early. And that is where result comes in. So result is another enum. And it's part of the prelude as well. And it has two types. One is the success result here, which is I32. And then the other one is the error type.
Speaker 1And here we're going to use string here, just for this example. In real code here, you don't want to use string. You want to create your own error, implement compile of traits, and then you can bubble things up. It's very nice. Result to panic, we're going to actually return the variant of the enum result, which is error. And here we need to give to string because the string needs to be on. And then obviously here we need to Return it. And finally, we cannot return the
Speaker 1result as is. We need to wrap it as well in a result enum and with a variant OK. That is a two variant of this enum. And now here, we're going to get our result, which is going to be of type result. And to display it here, we're going to use a debug notation. Press save. And now we get our error. And if we divide by four, we get our 2 back. And again here, the result
Speaker 1is part of the prelude, such as we don't have to import it. And actually, like options, the variants are also imported by default. So we actually do not need the result enum here. Press save. Works the same. And since the if-else can become an expression, you will do something like this. Put the return inside the else arm. And then you can remove the return and remove the
Speaker 1end semicolon. to make the if-else an expression. And that will return, since it's the last one of the block, will return the value. Press save, and it works exactly the same. So now the way we'll do the branching on the enum result here will be with actually match. So we'll do a match on the result of flow divide. And that will be our OK with the result, which now will be the I32, or the error which will
Speaker 1be the string. And now we can print our result here, and we don't need a debug because now the type is I32. And here we can println the error code. We don't need the debug again here because error is a string. Press save. Now we have two. If we divide by zero, now we have error cut. And then
Speaker 1that's it. That is a gist of result. And the backbone here of error handling in Rust. Now there is much more than that because usually as an error, you will put a myError, which will be your own type, which will implement some error traits. And then you can use question mark here to bubble things up. And it becomes very, very productive, fully typed, and very efficient. And that would be for our next episode. Until then, happy coding.
Building production-ready web applications requires efficient database interaction, flexible query construction, and well-structured data access layers. We examine how these technologies work together to simplify...
In this episode, we continue our exploration of nine essential Rust programming best practices, focusing on utilities, practical examples, and software testing. Building reliable Rust applications requires more than...
In this comprehensive tutorial, we explore how to build database-driven applications using Rust and SQLite, covering essential database concepts and practical implementation techniques. We take a hands-on approach to...
Join Jeremy Chone for a live-stream session dedicated to Rust programming, software development, and practical engineering discussions. This session provides an opportunity to explore Rust development through a live...
In this episode, we explore data access in Rust web application development, focusing on how backend applications interact with databases to store, retrieve, and manage information. Data access is a critical component...