DEV Community

Cover image for Rust: Initial thoughts

Rust: Initial thoughts

Henry Boisdequin on January 12, 2021

Recently, I have started to learn Rust. Why Rust you ask? Well, it's one of the best programming languages in each of these categories: performance...
Collapse
 
valeriavg profile image
Valeria • Edited

I've started learning Rust for the same reason, but because I'm coming from the same background of non-compiled languages (❤️ JS) and old habits are hard to give up, It's been a rough journey :-)

Does anyone have any hints for the code structure, for instance?

Collapse
 
yjdoc2 profile image
YJDoc2

Hey, can you explain what do you mean by code structure exactly? 😅😅
For the projects I have done with Rust till now, I usually start with a rough separation of responsibility in different files, and as it grows ,keep refactoring and breaking large files into smaller, more modular files if needed. As compiler is so helpful, and testing is quite easy due to Cargo, the changes made are quite easy to verify for stability.
Hope this helps a little 😅🙂

Collapse
 
valeriavg profile image
Valeria

Hey! Thanks for the reply!

I was wondering if there are architectural concepts on how to organize files for big projects in Rust.

In NodeJS, for instance, I like to create a modules folder and then I can manage features by adding or removing a reference from a barrel file.

These modules can then be merged into a GraphQL or REST Server. But that server then will have a bunch of resolver functions.

These functions take a fixed number of arguments, but they almost always have different types. And thus, defining a trait that all of the resolvers would comply with, renders impossible.

With this said, I understand why it should not work in a compiled language and that I'm stuck in the ways I used to do it.

So what are the Rust ways? :-)

Thread Thread
 
yjdoc2 profile image
YJDoc2

Hey, there are some parallels between the NodeJS structure and possible Rust structure :
Say I have to make complex module ABC, which has to export multiple classes, functions and all. One would make a folder named abc, in which one can split files for each of the classes, maybe even one per functions, and export individual element from each file. Then one would define an index.js file for that folder, which will import from all files, and export elements, which can be directly used by import statements in other parts of a project. (#TodayILearnt it is called barrel file 😄)

Similarly, one can define in folder abc, multiple .rs files for each element, where they define that element (struct/function/trait etc.) as public , using pub. Then one would create mod.rs file, analogous to index.js, which will have pub use statements instead of pair of import/export statements as in js.

Then in the other files, one can use the use statements for importing the elements that are exported by that module.
Note that this gets a little complected and/or different depending on type of the "project", as there is a differentiation between a binary project which can run on its own (an express server application) v/s a library project which is meant to be used as a library for other projects (express itself). One can also have a project that defines a library, and then also has a binary application in it, which uses the library. (Defining a middleware module for some processing, and using the same in an express server which is in the same project) Depending on the type, the path for use statement will slightly differ.

One way would be to look at existing Rust projects and seeing how they have structures the files, that how I decided for my project (Library + application) : github.com/YJDoc2/8086-Emulator/. The file structure part in Readme might make it a bit more clear.

As far as GraphQL goes, I do not have any experience, but there seem to be some ways to do it, as there were several blogs showing how to use GraphQL with Rust, so it might be very much possible.

Hope this helped a little 😅

All said, I am also still learning Rust, so maybe there is another way to do it which I don't know, and someone else might reply, from which I can learn too. So, thanks for asking the question 😄

Thread Thread
 
valeriavg profile image
Valeria

Thank you, will dig into it :-)

Collapse
 
leob profile image
leob

Apart from the "low level" and memory management aspect, there are two other things about Rust that I noticed when learning it:

  • a very sophisticated type system (supplemented by "macros"), which makes it possible to write "correct" software - that is, you can, to a certain extent, be assured that it will work if it compiles, even before you run it

  • strong support for FP concepts (Functional Programming) - many features in Rust reminded me a LOT of things I saw when learning Haskell

Collapse
 
hb profile image
Henry Boisdequin

That's very true! I especially noticed that Rust is more a functional programming language than an object-oriented one. This really popped out because Rust has no classes. I also think that Rust has a very sophisticated type system and many more powerful features (macros, traits, etc). Thanks for reading!

Collapse
 
leob profile image
leob

Correct! Rust has no (or very limited) support for "classical OO", like class-based inheritance, but support for functional programming is extensive, even though Rust is not a "pure" FP language.

Rust does have interfaces and traits and so on, but doing conventional class/inheritance based OO programming (like you'd do in Java) is not a natural idiom in Rust - an experienced Rust programmer would generally prefer other approaches.

Thread Thread
 
hb profile image
Henry Boisdequin

Exactly!

Collapse
 
epsi profile image
E.R. Nurwijayadi

Good article. Thank you for posting.

I'm a beginner in Rust. To help more beginner, I have made a working case example with souce code in github. With an about three parts article, to leverage hello world.

🕷 epsi.bitbucket.io/lambda/2020/11/2...

For example, concurrency in Rust is actually an easy thing to do. And also the code is almost as short as goroutine counterpart, as shown in below example:

Rust Concurrency

I hope this could help other who seeks for other case example.

Collapse
 
hb profile image
Henry Boisdequin

That's a great article! Thanks for reading!

Collapse
 
yjdoc2 profile image
YJDoc2

Hey, great article!
You might find the rustlings course useful to learn Rust , by hands on examples
github.com/rust-lang/rustlings
If you like to learn by Books, along with videos , check our Programming Rust by O'Reilly publication, it is really good.

Collapse
 
hb profile image
Henry Boisdequin

Wow, what a great tool, I'll have a deeper look into it! Thanks for reading!

Collapse
 
hillwilliams profile image
hillwilliams

Good article!
I also just started to learn Rust.
Currently, I consider this language for my pet projects but maybe in the future, I will shift to Rust on regular work as well. Although as far as I see right now there is not much job related to Rust, maybe I'm wrong.

Collapse
 
hb profile image
Henry Boisdequin

Since Rust became very popular recently their are fewer job opportunities than say C++. With that said, with more and more people learning about the power of Rust, more open source projects are written in Rust (e.g. Deno).

Collapse
 
ssimontis profile image
Scott Simontis

I really want to learn it for embedded device applications, as it would be so much nicer than C code. It's a lot to take in, however, so I keep losing motivation to push through and learn more :/

Collapse
 
hb profile image
Henry Boisdequin

Trust me, once you get the fundamentals, Rust is the best programming language to work with (hence why its the most loved programming language 4 years in a row).