DEV Community

loading...

Discussion on: Rust: Initial thoughts

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 :-)