Tauri - Rust, Js and Native Apps
Giuliano1993
Posted on February 12, 2024
Good morning everyone and happy MonDEV!☕️
As promised, after a softier week, I'm back bringing you something more substantial, always hoping to offer you some interesting ideas to develop some new projects, or just to know about some tools that may be useful to you in the future.
For those who have been following me not too long, they'll know that in 2024 I decided to learn Rust: I had been procrastinating for some time already, but at the end of 2023, I decided it was time to start studying it, so I got to work!
This preamble was necessary so that you know that both on various social channels, on articles and here in the newsletter, you'll start to see content related to the good old crab language! 🦀
So why not start today with something softer and perhaps close to other things we've already seen or are easy for us to use?
Today I'm talking about Tauri!
Do you know all the various tools that allow you to develop native applications starting from web languages? They often need an intermediate compilation, in the middle of which you end up encountering various problems not always transparent and directly solvable with a language mostly detached from native development. On the other hand, there's still the ease of developing attractive and easily usable interfaces, which are more difficult to develop with low level languages.
At least that's been my experience.
Tauri takes the best of both worlds: it allows you to build the frontend using a WebView, an object we're now quite used to dealing with mostly thanks to Electron, but also other similar frameworks that use it, which allows you to start a window that runs client-side code, allowing the use of html, css, and JS (Vanilla or associated with our favorite framework), while the backend is all developed in Rust, which allows compiling native applications without intermediate steps.
From your frontend, you can call your Rust functions with extreme simplicity.
A quick example (of which you can find a more complete version on this repo) of how communication between the front and the back on applications developed in this way happens is this:
// Processes.vue
//...
async function processes(){
// invoke the rust method processes() and receive a string
const processes_buffer = await invoke("processes");
// split the lines to receive our processes array
const processes_array = processes_buffer.split("\r\n");
//...
}
and in our backend, we'll have
// main.rs
//
#[tauri::command]
fn processes() -> String {
let mut processes = Command::new("ps");
let output = processes.output().expect("The process failed");
let output_string = str::from_utf8(&output.stdout).unwrap();
format!("{}", output_string);
}
Surely, if you've never worked with Rust, some small parts of the code above may not be very clear (but I hope to remedy this soon with some contents, for those interested); however, I'm sure the general sense is more than clear: we call the ps
command that allows us to get the list of active processes on our machine, read them, convert them to a string to make them available to our frontend.
Clearly, it's a very simplistic example, designed specifically to mainly show the communication between the two parts of the program. The fact remains that once ready, we can build our program and have the various .exe, .dmg, etc.
In a situation where web applications are becoming more and more complex, with Web Assembly slowly gaining ground, knowing low-level languages like Rust that allow us to develop low-level components and functionality for browsers as well as native applications locally, I think it's the winning (and fun, something that's always at the base of every new study I undertake 😉 ) choice. In this case, also with the plus of applications for whose interface we already have a wide range of skills!
This was the first time I mentioned Rust in the newsletter and in general in what I write: I plan to dedicate some time to it together with WebAssembly!
What do you think?
But above all, have you ever taken a look at Rust or knew about Tauri?
I'd like to hear your thoughts on this, so whether it's on Dev.to or on Linkedin or wherever you prefer, if you feel like it, let me know!
In the meantime, as always, have a good start to the week
Happy Coding 0_1
Posted on February 12, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.