Rust vs Javascript error handling comparison - Matthjass13/RustForTheWeb GitHub Wiki
How to use the demo
Preparations
- Clone the project
- Install node JS from here https://nodejs.org/fr
- After installing node js, go to the folder Your_Relative_path\RustForTheWeb\rust_vs_js\demo_js and do
npm install - Install rust and cargo from here https://rust-lang.org/tools/install/
Usage
Javascript Demo
- Run
node bug.jsin the demo_js folder and access the server via http://localhost:3000 - add user/1 to test to retrieve one user and then try user/3. You should see an error from the server
Rust Demo
- go to the folder Your_Relative_path\RustForTheWeb\rust_vs_js\demo-rust
- Comment one of the two get_user methods and do
cargo run
One of the method will do a compile error, the other one will run the server and handle the error
What does it demonstrate ?
It shows us that when using for example Javascript, you could theoretically put a server on in production and still have an error if you didn't thought about the user trying to retrieve an inexistent ID. This will return a 500 error from the server
In Rust, if you don't take into account the fact that null could be a possibility, Rust won't let you even compile the server and forces you to take taht into account and handle this potential error.