{"id":1518,"date":"2022-11-22T11:54:13","date_gmt":"2022-11-22T11:54:13","guid":{"rendered":"https:\/\/www.workfall.com\/learning\/blog\/?p=1518"},"modified":"2025-09-24T10:08:31","modified_gmt":"2025-09-24T10:08:31","slug":"how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database","status":"publish","type":"post","link":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/","title":{"rendered":"How to Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database?"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/gPohrx5c0plakoAqSBQkEk2DezlWXdRjmrz2qZNP3kXfVFYsMS7DmImTBjYnreBmI0KEu7OsLB0W_0Q478PtN4LKgYhA0IZK5NpdrKJF5XRaE1VKFdNZG8Uxty-dP0orsGj4vF0U7PrtVo1LdmW_th6bV5xopjofwAe5RhWN-qTPFcl8__cAkblHig8B9g\" alt=\"How to Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database?\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">For many years, C and C++ have been the main low-level programming languages. C and C++ require manual deallocation of memory to prevent memory leaks which are the major cause of security breaches in Software. Rust comes in to offer memory safety and at the same time speed of execution that matches that of C.<\/p>\n\n\n\n<p>In this blog, we will cover:<\/p>\n\n\n\n<ul><li>About Rust Programming Language<\/li><li>What is Ownership in Rust?<\/li><li>About Rocket Framework and Diesel<\/li><li>Hands-on<\/li><li>Conclusion<\/li><\/ul>\n\n\n\n<h2>About Rust Programming Language<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">Rust is a systems programming language that has been proven to be almost as fast as C. The big question would then be why have another language similar to C? Well, Rust is very different from C because it has inbuilt memory safety. When we talk about memory safety one would automatically think of a garbage collector, because that is what most high-level languages use, again sorry to disappoint, but Rust does not use a garbage collector it uses a new concept known as ownership which we will look at in a few.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">The major difference between Rust and a low-level language like C is inbuilt memory safety. In a language like C and C++, you have to allocate and free memory on your own which has not proven to be effective. Microsoft admitted that the major cause of security issues is memory leakages and they decided to replace some of their legacy code with Rust. Rust also has a package manager. It was also discovered that Rust programs consume less electricity. Yes, less electricity due to its efficiency, which may save the company tons of money spent on Cloud Resources.<br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/E1GL4tcpY9J1WD-2F6Wek9V_DwHlDgVRpdCVHupXrL80PiG3dCOcaq_9b2bAoxBmNsv6piUBwqca24zbb7mI7-M4FilTlCDi8oa7WWvp3pkcdiskEeXcH9HCJaRDuifNwSBhfpa1u5orAUEexo85jHYdylauHe41fqDRT9IGRJktKb0GQiLYyvfPykV2CQ\" alt=\"About Rust Programming Language\"\/><\/figure>\n\n\n\n<p>Did you know? In 2017, a team of 6 researchers from Portugal had run solutions for 10 programming problems in 27 different languages to monitor which language consumed the least electricity. The above image illustrates their findings.<\/p>\n\n\n\n<h2>What is Ownership in Rust?<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/Dfof76YFRDt94R7cEnpXDHV_oZEhg7fqSezIZnFJvLLlYB6OugbPnome9O4zA-Wvi2uDuwr-bl361K1XjJnyHyLweMR_espoi4mL19x3WjowBLljrYU2sQ284Ndmc2HOObobosTtBbkoQLYoIwc5buYkeBwZf_G-LfCGQgEOQmahNyiDiSuNWlYIwoGJJg\" alt=\"What is Ownership in Rust?\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">As soon as a variable goes out of scope, it is dropped i.e its memory allocation is freed. Let us look at ownership in code but before that, important things to note are:<\/p>\n\n\n\n<ul><li>Passing a variable as an argument to a function will either <strong>move<\/strong>(transfer ownership) it or <strong>copy<\/strong> it.<\/li><li>Reassigning a value to a variable will either <strong>move<\/strong>(transfer ownership) it or <strong>copy<\/strong> it.<\/li><li>Variables that are <strong>copied<\/strong> by default implement the <strong>Copy<\/strong> trait, most of which are primitive types and Immutable references i.e &amp;T.<\/li><\/ul>\n\n\n\n<p>i32 is a primitive that implements Copy trait.<\/p>\n\n\n\n<p>The figure code below shows the same (how ownership works in Rust):<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/qt7fIX0vu5wTlPEFynQpbR3CYqDGZ5WH2oPiL4mivr0dlY9d6iUASNhHze5RmORL0XAdklxorrJeSA2by2rUxYUW_ijMo6SZCG768fQrhn10HILdyOM_PkpiYNq8RBffZT9MBGwf5zSsiluzoSL94vWDNV3ZbRHEwIce1w8jFgbpp6aag1t9NNssufU1hw\" alt=\"Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/Wr974d_mGb2UDHABnNEVtbP52xKa0-4DxZiJ8hgd-vP8VTYQ4RSlBhyYIp4twZYhA9oF3akHI31l2lpRyKMnKt8mH0TiStLrPQVo4QQitASxT1spXTiuvv2eFI_MvhGevE4y6S4gkkhM6tEgKOBBIwB02kw0xLuD4s0lFCgWb2Kc42sTlz5FQ6j1eJIxRw\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/0vy7wRcXR_AGdYs672OrB8Iyr51csSDrE23aGS3D9iqIj0lFtmylLdVpjzUqEQS5cgasf9LE_t6p9RBCsaeostQvqSJQy9dao5XoIcPtELzFn43ZSsLwED6Yg8NlgPDKAPI3GBwMZe8bNrUKNgivH0A8FOdyPhHZz8UCq6RLI7xLBDTpbgkwC_4n9ggzag\" alt=\"Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database\"\/><\/figure>\n\n\n\n<h2>About Rocket Framework and Diesel<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">Rocket is a Rust web framework that makes it easy to develop web applications. Since Rust is a compiled language the end result is an executable which is a plus. Diesel is an ORM built with Rust and it enables us to interact with relational databases by providing a layer of abstraction.<\/p>\n\n\n\n<h2>Hands-On<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">We are going to create a simple REST API that will allow us to add users to the Database and assign them roles. The repository is <a href=\"https:\/\/github.com\/workfall\/workfall-rocket-rs\">https:\/\/github.com\/workfall\/workfall-rocket-rs<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/6UtpGtSRoO-h3sJ3BORpOJiTAmRMucJUFFAi4Ws5Qc18-zuIvd7co-DQrGvXd9XXMy7Fp3YHTjFpRqqmS6C-o7TeztZ95q9yTyJXcmmiWeBwcFjs0obLVT-4XNwdAel1NN0QLHxX8a3Y-ERwmnIL0Bw3i46ObuIeqHlLv0nGj4_8QUJRnODgnPKniuig_Q\" alt=\"Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database\"\/><\/figure>\n\n\n\n<h3>Prerequisites<\/h3>\n\n\n\n<p>You should have the following installed on your computer:<\/p>\n\n\n\n<ul><li>Rust lang &#8211; Comes with cargo which is a package manager for Rust. Rust packages are known as crates.<\/li><li>PostgreSQL Database server<\/li><li>Diesel CLI &#8211; This is installed directly on the system with the <strong>cargo install diesel_cli &#8211;no-default-features &#8211;features postgres<\/strong> command.<\/li><\/ul>\n\n\n\n<h3>Initialization and Project Structure<\/h3>\n\n\n\n<p class=\"has-text-align-justify\">We shall start by creating a new project using cargo, the command is <strong>cargo new hello-rocket &#8211;bin<\/strong> we are using <strong>&#8211;bin<\/strong> because we want the compiler to create an executable hence the <strong>main.rs<\/strong> file. The other option is <strong>&#8211;lib<\/strong> which is a library or in simple terms code which can be used by other crates hence the <strong>lib.rs<\/strong> file. In this project, we will use our diesel middleware as a library.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">The <strong>Cargo.toml<\/strong> file contains information about all our dependencies. Allow us to look at it like a <strong>package.json<\/strong> file in Node.js.<\/p>\n\n\n\n<p><strong>Cargo.toml:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/GEJ3PZvIhsHVrWLBuV_vQ4ziiHjNPWMJzey9GGJOsr8zygXBtxgqcBl5RBEgsPlpVEg85P6qoxBXBOTkAjosoWOsXYFo-e-RpbhiAFzU5TEJm7vIhi6S1y9e-PCy8mK-zEneMJaxAJUzHO4J0eVlb-I3sWndtYuSDkcN29P_lWSDy6FVtJ6wS25igdLZKg\" alt=\"Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">The <strong>dotenvy <\/strong>crate allows us to use a <strong>.env <\/strong>file to manage our environment variables, <strong>serde<\/strong> crate allows us to serialize and deserialize JSON in our code and <strong>bcrypt<\/strong> to encrypt our passwords.<\/p>\n\n\n\n<p><strong>Project Structure:<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">Note: Some files are auto generated so don\u2019t sweat yourself creating them. i.e the <strong>migrations<\/strong> folder, <strong>diesel.toml<\/strong>, and <strong>schema<\/strong> file.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/lh6.googleusercontent.com\/-rsGjUQDxSI-ofFM7Ph73TSrGBaOSiJvU0Lun1P60Jp-Ot39zDOor9Hx_Zu63FrCS1EeicWM0gYUT1X4qm-cI_d0yXrSrcKUelZhIWK8rmB1w0tN4dXV568adxJHsZdxaStwJmF2v6VttraTttKlcWj8IoWkAu-6Lcck-IAuFT4iH6DamWGfGxNoHJLvFw\" alt=\"Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database\" width=\"347\" height=\"627\"\/><\/figure>\n\n\n\n<p><strong>.env:<\/strong> The <strong>.env<\/strong> file will appear as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/b5JuzbgYtBDssgI12X6h8U4FMjHUe-DC7gptGHZMS5CMLyOONnlljL_tZGWQpIp8V_9a7ibzNf4qNF_VFqbY2M1tnd3xfNu_pe7wF817Do7Uw7Ucdr-v1wkUESn5FBlOyHotoRgg4jP9gatdWk4xG90arzdEhhtdObP0JNSj6bz4pQE-9hsBdDWe82_LbQ\" alt=\"Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Next, we run <strong>diesel setup<\/strong> which will create the database for us if it does not exist and it will also create an empty migrations folder and a <strong>diesel.toml<\/strong> file. To run a migration use <strong>diesel migration generate &lt;migration_name&gt;<\/strong> I prefer the <strong>migration_name<\/strong> ordered with incremental numbers in order of relationships because errors might occur where we have joinable tables because the migrations are executed according to folder arrangement. In this case, we only have two tables, users and roles.<\/p>\n\n\n\n<p><strong>diesel migration generate 001-create-role<\/strong><\/p>\n\n\n\n<p><strong>diesel migration generate 002-create-user<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">In this case, it makes sense to create a role first because every user will be required to have a role. This will generate <strong>001-create-role <\/strong>and<strong> 002-create-user<\/strong> which will have <strong>up.sql and down.sql<\/strong> files each. The <strong>up.sql<\/strong> file contains the SQL queries describing the table columns while <strong>down.sql<\/strong> contains a SQL command to drop the table in the corresponding <strong>up.sql<\/strong> file.<\/p>\n\n\n\n<h3>001-create-role<\/h3>\n\n\n\n<p><strong>up.sql:<\/strong> The <strong>up.sql<\/strong> file contains the SQL queries describing the table columns.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/k6Rna-xIdFseWahXs5CQUEDpD8NEb9V0VlZE4uPN1ZeURu0tG2BR9d1Z5DdxY3pTK6g_Xpn38eZsYI_VN75nhP_lkUFEAF9gsTNiRTXGCYIX2yR86Fo0LDkwsO8B1sZKffaHX8uCoZanbnQ4DWRy3RfptLVRAnR1JPbV5la0uEpo06cr7M7BG_jQwy3g2g\" alt=\"Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database\"\/><\/figure>\n\n\n\n<p><strong>down.sql: <\/strong>contains a SQL command to drop the table in the corresponding <strong>up.sql<\/strong> file<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/Qtjq8rrHJZI7WI834GacZJAeYMXyfztW8z_xNgASSvab1QGNZUbRh3BY4_JIrt6t2Sqwb67bzXaX6DoONJNcq41fpzyP1y_if7RySs6Df5_WR3IuPhMKCwsjQ0LEQYGhInIe8STEu9u47NfL8LE0dznoVu_HrgRwpC28Oo0KLnDgKdcZ18jtchf6B0pXGA\" alt=\"\"\/><\/figure>\n\n\n\n<h3>002-create-user<\/h3>\n\n\n\n<p><strong>up.sql:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/oIWher4vF8ID9kBwwl86zd4IrAI4Kwp8BvwsFydNL26cHdf7BSXfs_4jxDs1Txjc4eipUH5beyeKjulvRpdmUHXiC_JkX4jmKh5BWDrZ2rjILi_t0NVHYbCOp1OgvQ26Nj390NOgk2_B6TKmrz-AoHpziZoz9O2wEVcTZw6v_UgYRNZSIbDuehzqZnVznw\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Down.sql:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/_Oh4kAwHmazC7yLV_e1gP9yXaNUF_ggS9hwgSHgk4-7oI0x1-nkKZ7FOSDNCKxnqc0ziRnHXodAbr5WT3LZwgjvEMAoUFKe7PNqKm0or5H1gAH2--oFDtD9aUJjIvZxOIfdXhbW1yfYV1KCcmZKh_xhPAeqP-WwKmsG6oGeZ1I-cIHZMkunUqm4a2ZBcOw\" alt=\"Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">To run the migrations use <strong>diesel migration run<\/strong> and to redo the migration i.e drop then recreate the table use <strong>diesel migration redo.<\/strong><\/p>\n\n\n\n<h3>schema.rs<\/h3>\n\n\n\n<p>This file is generated automatically when the migrations run successfully.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/BCN0zetxbrh7x0GvBSDgb0Wx0YtWA4RuQnF3ZzInmtJxDuziUneCB5cziEx6aIaqP25JpJ3sRh67LmrU8hDVaNE-woSuJNkgaCjcQrAjuutuWgujPM9GFZ4LvC4Jh2w-faMOBN1oyCshGfXf5Ojwny0R3kxs9Lj0Mc3aRWpHihPrE2SNfE_-mYmJbAdfYg\" alt=\"Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database\"\/><\/figure>\n\n\n\n<h3>lib.rs<\/h3>\n\n\n\n<p>Contains the entry point for our library and also imports other modules that are needed by our library.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/yMKNkkgM52kjgxyyKMmvRIZ0fZUQD8ML9Yf12aBVd22uZoQL85zAkQtVdaClWNGufPi01bEIsRcpr1Vj-OV8uXzM_qc40X9Z10LZfZv4fzqulq40Ebd7YKnX1LOF-QzcQP3ZnH1glibyLjkpv3JfW6trq-g1v4mognesgk9cBAVxpr0-QDjNqJV3yglK8w\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">We can access modules from our library <strong>lib.rs<\/strong> by using <strong>workfall_rocket_rs<\/strong> which is simply the package name as in <strong>Cargo.toml<\/strong> just with underscores used in place of spaces and\/or hyphens.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">In our folder structure, we are using each folder to represent a module. The Rust compiler knows that in every <strong>mod.rs<\/strong> file, we have a module(s). So we have routes, models, and services as modules.<\/p>\n\n\n\n<h3>main.rs<\/h3>\n\n\n\n<p>Is the entry point of our Rocket application.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/RZIghl6djSgw6ZXMv0XjXzfVCaixHAI5sCIIw5BfZfQH0aenJ8XnTiVZjq-tgSnx543gtAdNWvx7wJ2FGQqWrDNmDHSIRPMYljQ36U4xCy4CDfDPuCWpgXSzIb_wJF737AMAQ5vJVP1Vfe-rpj2PSbUj2EmqWN8ApCsf2UfLChJj31_sw26G0ddRrqduyw\" alt=\"Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">The <strong>main.rs<\/strong> file resolves all our routes and services and also contains some special Rocket catchers to catch some errors and give back the response. Disclaimer: Error handling was not well covered in this post.<\/p>\n\n\n\n<h3>Functionality<\/h3>\n\n\n\n<h4>Models<\/h4>\n\n\n\n<p>These describe our data and also create a basis for serializing and deserializing JSON.<\/p>\n\n\n\n<p><strong>models.rs:<\/strong><\/p>\n\n\n\n<p>Contains structs that describe our Database models.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/8eT_xXIKeHSqNTDd3fl-xUtpwpPryt9Xvk5Db97kAn-whiruF0NQbJPWuPKy7hHEu5MQ13Vh9lsiw0_P87e-8wt3NUS4KGfe317V95r8nL0VzI5JCXlGEzmB0lTQafPaGpcwljZYxv-wGtO6vUk9Q2AlJTugY6n58c5-Kr8oSlqz9JOEjQHAEs4LSwNHow\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>continuation of model.rs<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/gVnqEDzmUEfYZZMUI8tuyJx-h67Ar9bagUWL2sOQph8Z25oDZGrVTcjllH20ybL088EIaeFHCQw_D4n9Lk7MnTAqn_p7Ue5HsosjjrZU_AeSZUICXdYiGGu2wB0M6gvlRJZYGDJHNEf3KKA6DxyKYJ5fdtS1ylAi3YkAApx2CjRQM0UTbcLEQMgplTLrDw\" alt=\"Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database\"\/><\/figure>\n\n\n\n<p><strong>mod.rs:<\/strong><\/p>\n\n\n\n<p>The entry point for the <strong>models<\/strong> module.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/Cj8oOKYeM_INwE-sMEqAnFRc8P2qvhAXJa6PJ9EYyZIXIFu4eqO7AzyNMF5PHZm3rntLdJu02GHDt-yYUwGHV1Str4qtzHFcBVvUrF-AXJ2079FUAc8GjP1fBRL7jtG4La4Q1W-YHecPUlBc6XptU6K8eQshzIFZX_hMlms4LIFwtuIoYpNKfRcPlf652A\" alt=\"\"\/><\/figure>\n\n\n\n<h4>Services<\/h4>\n\n\n\n<p>Services module contains all the logic that is consumed by the routes module.<\/p>\n\n\n\n<p><strong>index.rs:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/iByDSW1zvtIir8cMeY00aVo8EVfGQEOQHsByKhtHaC4Z3VWzJUhVm4W3ACWRrzvvmyFNXezdLZCQF9xz8BsHZGjdXCSQWNF6MH90EX50P33ZVPphlkfq9eRuRiO-BoihcOMIvgn9g7056oJ3aEKqluhUTXhP1kjVm8mAQdzhmexHhlQGtY2iwIXG_Y95oA\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>users.rs:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/oFykuy9tSL7wbnaFy16Y6TKT8rL27L50k_S9grVLoO-T_1WKEdH67KfZeR6wfS-we4vnpnIfQE95wNLGJS4ssLiChjwvmZyUwmDIDJJbipgLWA2TZSFR1bzVlq-6-9luYMPQV-L2vCEnGYDX6ODzQb4ph7Dmzd61TkZWwkXQzyW9Nnf6-kzrNym472eUKw\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>users.rs<\/strong> continuation<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/f3oPE--mb8ohn0Z7TNYtsPRQC9fsYyZgcdwJKtgqokWg1YD84j46rM_9S_iDZqweCDAizsSrCOSDkqY7HqHvSP_NN3svX0LTm_M_VVAPKuyy7s9iLdLIxOHJp3Ctw34cPFjZBMHj0DRYxbeEqt9mWeUuLGJ1VS8zlU8bkyrFsRmGPdoZTtJH9eFiCE5FGg\" alt=\"Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database\"\/><\/figure>\n\n\n\n<p><strong>users.rs<\/strong> continuation<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/1h6IpzWael--LP0ydQDDJld0cqqPOOuuNs-zlL81E5VYDjKVWC3WgeLq8slKOCkON-3tLDq7VKAzkMAA5t9Td66PlFHaWGWpZeVQtDcb4jRMiWyIB9a-NiKg8sWrm8nL4uYvSiEY9s51RDyfnQf3bl4995J5cre8cb1Y-8LMdlIaw7DENSMXiGLp_trRWg\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\"><strong>.clone()<\/strong> used all over this code is used to create a copy of a value and <strong>.unwrap()<\/strong> is used to resolve a value for a variable which is \u201coptional\u201d i.e implements <strong>Option&lt;T&gt;<\/strong> or can be as you would say in JavaScript \u201cnull\u201d. <strong>.unwrap_or()<\/strong> provides a default value if \u201cnull\u201d.<\/p>\n\n\n\n<p><strong>mod.rs:<\/strong><\/p>\n\n\n\n<p>The entry point for the <strong>services<\/strong> module<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/qfzW83J4AhQwmZ--sKeGm6FVvObOmcNixMSrm8TX-KuLH0GpkR4GXGkNqQtW9s-n6Ec0e_vCzUylDI30nbfIdSoCenLhvVVHItSqUC4xJxUGo0yNpW3C3RA-LLNp1Vc3qcLpaDkwmAQrPVC6ub6-t7EZAYXRSlEd7uE32huTRzKyL0984lq19F46GacFyw\" alt=\"\"\/><\/figure>\n\n\n\n<h4>Routes<\/h4>\n\n\n\n<p>A module that houses API paths\/endpoints for the application.<\/p>\n\n\n\n<p><strong>index.rs:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/2HIsCv8VoV33gszqjQOD5YfZOJwpfAK5ZX3QXCSRTzRCyfNPBJMg6s3WfxvLp42onmrcQJ69TC7oPG68Bgz9-qAvl9Ic8sT74SiMA0OJVm3RBuhNpcNuDeYa-Gj_sD5Nty5wqHAaDk8rxjI5MZwLcoITXVd-KTncarTPC7k7GUySQDeOQf6pkG_QyLmgjw\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>users.rs:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/XzQbserYetzum6X6u8hJvpmpB6aJG3qc7vOHHB9BfuxT_SdQC-nOO10OBjxMAZwLmc6J2yr9pG0GFDd0PBHtkuBpD27tTzsE_JCsbXbHWy7u7aYkTHRdudqFN5_NaL_rH6PSjNxeNWntkpS-EOgObvvFQarZTC_JI_NobYrSidO_bQUy-IzOoeQrGNRylg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>mod.rs:<\/strong><\/p>\n\n\n\n<p>The entry point for the <strong>routes<\/strong> module.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/maw5wfqfoB8albhOSwhVpmF8CdhxWnYXoROi2bPLl_yEySZMdPCRcDSqu9FSJiLeTHagPe4XIxpVuAmGcIaIXgAqqLys0XHyjQ3U_Ppx0OAgr3rDnyabKAK-DhCFgg9UO2MRcSkd4qUVVVeoADIcQ0taAbNfHpeFr6LHRxZVNLzADuU7SDHziCSAptCFQw\" alt=\"\"\/><\/figure>\n\n\n\n<h3>Dry Run<\/h3>\n\n\n\n<p>To run your application in development mode, use the <strong>cargo run<\/strong> command.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/TPr1Ygq9HnY8YngtU8XmDbVEzT6FW3uQMA6jPbYNiGL7HY8GKezcmvUVem9JGN3APnD4n-k-elVXIfz6XKUA-Uz_8DhJmmdXHs83IBoggqbqRL-Z94DxTuPH48L8KA3S0OmELMNcBM5K2gGV7s7CjrU8YByKLQ9JkCTzZMx6SQWiCivlEd0VMnJfweWWKQ\" alt=\"Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database\"\/><\/figure>\n\n\n\n<p><strong>Adding a role:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/rxt5r6NFBtjgHM81yePs71tskfMii7ioYo2e4VRbAG7Gwiv0nxV7EX5CCbdpvczHrWpBlgPq2cBeI-9IQCJ49YKCaEkAEx0lLizUlef9nV9llKFjP2u6vj4K7eoXFvfU7InCQnuR85UlkzKCBKJceq5Z77nROhGq4t3IrRDlBiZ3ejaM8SLo4xB8U_ZNTw\" alt=\"Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database\"\/><\/figure>\n\n\n\n<p><strong>Adding a user:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/eqUZKfPBItJTw8WV5HyVGyCoDy1NIjUV7glUa1JxE4r405Rhvm8Qvrg9hS93IpJ7NuBCf7Oaxo6YVGb2HQ-8-kLlRfESZwgyuijfDlYCHXRTj3ruG_t3WQU0ZYxGhJxH8nNIwNBvi4ppH68Zhy8_uQ0q7U8Hx6Dpo6JSFQKKzP64vL2DwzN3Xm7Dq298zw\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Getting a list of users:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/jLB3z-f02FrMI4w56rDoH4j0P7q9bCWGgpwkuvNLJEamgYEbjh44HFG6Ln0JTg97Kad7gm_Gg-XVvQ0TnI0w2FtzTA861yxzFPlPHiv5CB7pBGKnAOd-Ly3avkWFc-wXcgqZy-YMRWlPYRUzAlvVCDGlTdVD2VpHUN-8GhNwsP_z1wenDJI7wk8fw5BftQ\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Editing user info:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/s5nWh8bo18-zqrjZ-7OMirN2VBCY-S-9gPtTlE5C-_JDCFIXdVQXZBtM0WD6JYaGFDub017Y-_4kgeVvMyz0mNGHdrSkn2f8Z7hQ_BJFNeSza4I1G8mAd6sO8pRJ5bAjZqmREsWu8BUtDOjXtd789YoDyFObnNpDekEni3Smvcawda_4vtihHV_RQjyY_w\" alt=\"\"\/><\/figure>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">In this blog, we have demonstrated how to create a REST API with the Rust Rocket framework and Diesel ORM. Rust is a good language with quite a unique syntax. It is recommended that anyone who wants to learn the core syntax should look at the official documentation. In the next blog, we will come up with Rust, and dive into better error handling and also deployment in production &#8211; AWS for that matter. We will come up with more such use cases in our upcoming blogs.<\/p>\n\n\n\n<p><strong>Meanwhile\u2026<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">If you are a Rust enthusiast and want to explore more about the above topics, here are a few of our blogs for your reference:<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/www.workfall.com\/learning\/blog\/aws-is-in-love-with-rust-and-you-should-be-too\/\">AWS is in love with RUST and you should be too!<\/a><\/li><li><a href=\"https:\/\/www.workfall.com\/learning\/blog\/how-to-migrate-rds-mysql-database-to-rds-postgresql-database-using-aws-database-migration-servicedms\/\">How To Migrate RDS MySQL Database To RDS PostgreSQL Database Using AWS Database Migration Service(DMS)?<\/a><\/li><\/ul>\n\n\n\n<p><strong>Keep Exploring -&gt; Keep Learning -&gt; Keep Mastering&nbsp;<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">At <a href=\"https:\/\/www.workfall.com\/\">Workfall<\/a>, we strive to provide the best tech and pay opportunities to kickass coders around the world. If you\u2019re looking to work with global clients, build cutting-edge products and make big bucks doing so, give it a shot at <a href=\"https:\/\/www.workfall.com\/partner\/\">workfall.com\/partner\/<\/a> today!<\/p>\n\n\n\n<p><\/p>\n\n\n<style type=\"text\/css\"><\/style><section id='' \n                class='helpie-faq accordions faq-toggle open-first groupSettings-516__enabled' \n                data-collection='' \n                data-pagination='0' \n                data-search='0' \n                data-pagination-enabled='0'\n                role='region'\n                aria-label='FAQ Section'\n                aria-live='polite'><h3 class=\"collection-title\">Frequently Asked Questions:<\/h3><article class=\"accordion \"><div class='helpie-faq-row'><div class='helpie-faq-col helpie-faq-col-12' ><ul><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2935\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2935\"\r\n                data-id=\"post-2935\" \r\n                data-item=\"hfaq-post-2935\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. Why choose Rocket + Diesel over other Rust frameworks or ORMs?<\/div><\/div><div id=\"accordion-content-post-2935\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2935\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><b>A: <\/b><span style=\"font-weight: 400\">Rocket offers a strongly typed, ergonomic web framework with macro-based route definitions and compile-time safety. Diesel is a powerful type-safe ORM for PostgreSQL (and other SQL DBs). Together, they allow building APIs where many errors are caught at compile time (e.g. schema mismatches). If you prefer less compile-time complexity or more dynamic flexibility, alternatives like Actix Web + SQLx, or warp + sea-orm might suit better.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2936\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2936\"\r\n                data-id=\"post-2936\" \r\n                data-item=\"hfaq-post-2936\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. How do I handle database migrations and schema changes?<\/div><\/div><div id=\"accordion-content-post-2936\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2936\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><b>A: <\/b><span style=\"font-weight: 400\">Diesel has a built-in migrations mechanism. You define SQL (or use the Diesel CLI) to create up\/down migration files, which you run to bring your DB to the desired schema. Always version your migrations, test rollback, and in production deploy migrations before or along with your new application. Also back up your data before doing destructive migrations.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2937\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2937\"\r\n                data-id=\"post-2937\" \r\n                data-item=\"hfaq-post-2937\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. How to manage errors and responses in Rocket + Diesel for a REST API?<\/div><\/div><div id=\"accordion-content-post-2937\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2937\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\">\u00a0<\/span><b>A: <\/b><span style=\"font-weight: 400\">Define custom error types (or use existing ones) and implement <\/span><span style=\"font-weight: 400\">Responder<\/span><span style=\"font-weight: 400\"> for them, so your API can convert internal errors to HTTP responses (e.g. 400\/404\/500). Use <\/span><span style=\"font-weight: 400\">Result&lt;T, YourError&gt;<\/span><span style=\"font-weight: 400\"> in route handlers and <\/span><span style=\"font-weight: 400\">?<\/span><span style=\"font-weight: 400\"> propagation. For database errors, map Diesel errors (e.g. <\/span><span style=\"font-weight: 400\">NotFound<\/span><span style=\"font-weight: 400\">, <\/span><span style=\"font-weight: 400\">UniqueViolation<\/span><span style=\"font-weight: 400\">) to appropriate HTTP status codes. Also, you may use JSON error envelope formats (e.g. <\/span><span style=\"font-weight: 400\">{ &#8220;error&#8221;: &#8220;message&#8221;, &#8220;code&#8221;: 400 }<\/span><span style=\"font-weight: 400\">) for consistency.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2938\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2938\"\r\n                data-id=\"post-2938\" \r\n                data-item=\"hfaq-post-2938\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. How to structure the project (modules) for maintainability?<\/div><\/div><div id=\"accordion-content-post-2938\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2938\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><b>A:<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400\"> Common structure:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400\">src\/main.rs<\/span><span style=\"font-weight: 400\"> \u2014 Rocket setup, attach routes, launch<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<li><span style=\"font-weight: 400\">src\/routes\/\u2026<\/span><span style=\"font-weight: 400\"> \u2014 route handler modules<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<li><span style=\"font-weight: 400\">src\/models\/\u2026<\/span><span style=\"font-weight: 400\"> \u2014 Diesel schema, struct models<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<li><span style=\"font-weight: 400\">src\/db\/\u2026<\/span><span style=\"font-weight: 400\"> \u2014 connection pool setup (e.g. <\/span><span style=\"font-weight: 400\">r2d2 + PgConnection<\/span><span style=\"font-weight: 400\">)<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<li><span style=\"font-weight: 400\">src\/errors\/\u2026<\/span><span style=\"font-weight: 400\"> \u2014 error types and conversion<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<li><span style=\"font-weight: 400\">src\/migrations\/\u2026<\/span><span style=\"font-weight: 400\"> \u2014 Diesel migrations<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"> This modular separation helps scale, testing, and readability.<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><\/li>\n<\/ul>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2939\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2939\"\r\n                data-id=\"post-2939\" \r\n                data-item=\"hfaq-post-2939\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. How to manage connection pooling and concurrency?<\/div><\/div><div id=\"accordion-content-post-2939\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2939\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\"><strong>A:<\/strong> Use a pool (e.g. Diesel + <\/span><span style=\"font-weight: 400\">r2d2<\/span><span style=\"font-weight: 400\">) to maintain multiple PostgreSQL connections. Configure max pool size based on expected load and DB resources. In Rocket, request guards can provide pooled connection references to handlers, ensuring reuse. Avoid opening a new connection per request. Also watch out for blocking DB operations \u2014 consider running long DB tasks in background threads or using asynchronous wrappers if needed (depending on Rocket\u2019s async support).<\/span><\/p>\n<\/div><\/li><\/ul><\/div><\/div><\/article><\/section>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span> For many years, C and C++ have been the main low-level programming languages. C and C++ require manual deallocation of memory to prevent memory leaks which are the major cause of security breaches in Software. Rust comes in to offer memory safety and at the same time speed of execution that matches that of C. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1519,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[288],"tags":[27,362,363,361,364,51,97,360,359,6],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database? - The Workfall Blog<\/title>\n<meta name=\"description\" content=\"Rust comes as a chosen language when it comes to creating REST APIs as it has no memory leakages, uses less storage &amp; helps save up on costs.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database? - The Workfall Blog\" \/>\n<meta property=\"og:description\" content=\"Rust comes as a chosen language when it comes to creating REST APIs as it has no memory leakages, uses less storage &amp; helps save up on costs.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/\" \/>\n<meta property=\"og:site_name\" content=\"The Workfall Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/workfall\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-22T11:54:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-24T10:08:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@workfall\" \/>\n<meta name=\"twitter:site\" content=\"@workfall\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Workfall\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#organization\",\"name\":\"Workfall - Hire #Kickass Coders On Demand\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/workfall\/\",\"https:\/\/www.linkedin.com\/company\/workfall\/\",\"https:\/\/facebook.com\/workfall\",\"https:\/\/twitter.com\/workfall\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400\",\"contentUrl\":\"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400\",\"width\":400,\"height\":400,\"caption\":\"Workfall - Hire #Kickass Coders On Demand\"},\"image\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#website\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/\",\"name\":\"The Workfall Blog\",\"description\":\"#Tech #Remote #Jobs\",\"publisher\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learning.workfall.com\/learning\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#primaryimage\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-1.png\",\"contentUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-1.png\",\"width\":1200,\"height\":628,\"caption\":\"How to Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database?\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#webpage\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/\",\"name\":\"How to Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database? - The Workfall Blog\",\"isPartOf\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#primaryimage\"},\"datePublished\":\"2022-11-22T11:54:13+00:00\",\"dateModified\":\"2025-09-24T10:08:31+00:00\",\"description\":\"Rust comes as a chosen language when it comes to creating REST APIs as it has no memory leakages, uses less storage & helps save up on costs.\",\"breadcrumb\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/learning.workfall.com\/learning\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database?\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#webpage\"},\"author\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a\"},\"headline\":\"How to Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database?\",\"datePublished\":\"2022-11-22T11:54:13+00:00\",\"dateModified\":\"2025-09-24T10:08:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#webpage\"},\"wordCount\":1492,\"publisher\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-1.png\",\"keywords\":[\"Database\",\"diesel\",\"middleware\",\"orm\",\"postgre\",\"postgresql\",\"REST\",\"restapi\",\"rust\",\"workfall\"],\"articleSection\":[\"Backend Development\"],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a\",\"name\":\"Workfall\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png\",\"contentUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png\",\"caption\":\"Workfall\"},\"sameAs\":[\"https:\/\/www.workfall.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database? - The Workfall Blog","description":"Rust comes as a chosen language when it comes to creating REST APIs as it has no memory leakages, uses less storage & helps save up on costs.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/","og_locale":"en_US","og_type":"article","og_title":"How to Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database? - The Workfall Blog","og_description":"Rust comes as a chosen language when it comes to creating REST APIs as it has no memory leakages, uses less storage & helps save up on costs.","og_url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/","og_site_name":"The Workfall Blog","article_publisher":"https:\/\/facebook.com\/workfall","article_published_time":"2022-11-22T11:54:13+00:00","article_modified_time":"2025-09-24T10:08:31+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-1.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_creator":"@workfall","twitter_site":"@workfall","twitter_misc":{"Written by":"Workfall","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#organization","name":"Workfall - Hire #Kickass Coders On Demand","url":"https:\/\/learning.workfall.com\/learning\/blog\/","sameAs":["https:\/\/www.instagram.com\/workfall\/","https:\/\/www.linkedin.com\/company\/workfall\/","https:\/\/facebook.com\/workfall","https:\/\/twitter.com\/workfall"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400","contentUrl":"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400","width":400,"height":400,"caption":"Workfall - Hire #Kickass Coders On Demand"},"image":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#website","url":"https:\/\/learning.workfall.com\/learning\/blog\/","name":"The Workfall Blog","description":"#Tech #Remote #Jobs","publisher":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learning.workfall.com\/learning\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#primaryimage","url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-1.png","contentUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-1.png","width":1200,"height":628,"caption":"How to Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database?"},{"@type":"WebPage","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#webpage","url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/","name":"How to Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database? - The Workfall Blog","isPartOf":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#primaryimage"},"datePublished":"2022-11-22T11:54:13+00:00","dateModified":"2025-09-24T10:08:31+00:00","description":"Rust comes as a chosen language when it comes to creating REST APIs as it has no memory leakages, uses less storage & helps save up on costs.","breadcrumb":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/learning.workfall.com\/learning\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database?"}]},{"@type":"Article","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#article","isPartOf":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#webpage"},"author":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a"},"headline":"How to Create a REST API with Rust Rocket Framework and Diesel Middleware with PostgreSQL Database?","datePublished":"2022-11-22T11:54:13+00:00","dateModified":"2025-09-24T10:08:31+00:00","mainEntityOfPage":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#webpage"},"wordCount":1492,"publisher":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#organization"},"image":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-a-rest-api-with-rust-rocket-framework-and-diesel-middleware-with-postgresql-database\/#primaryimage"},"thumbnailUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-1.png","keywords":["Database","diesel","middleware","orm","postgre","postgresql","REST","restapi","rust","workfall"],"articleSection":["Backend Development"],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a","name":"Workfall","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/image\/","url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png","contentUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png","caption":"Workfall"},"sameAs":["https:\/\/www.workfall.com"]}]}},"jetpack_featured_media_url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-1.png","jetpack-related-posts":[{"id":1671,"url":"https:\/\/learning.workfall.com\/learning\/blog\/use-surrealdb-to-persist-data-with-rocket-rest-api\/","url_meta":{"origin":1518,"position":0},"title":"Use SurrealDB to Persist Data with Rocket REST API","date":"March 21, 2023","format":false,"excerpt":"Databases are essential in web development for organizing data in various forms and shapes (both structured and unstructured). Their ultimate goal is for the stored data to be easily retrievable, updated, queried, and generally administered via a graphical user interface (GUI), dashboard, or even command line interface (CLI). We can\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"SurrealDB","src":"https:\/\/i2.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/03\/Cover-Images_Part2-1-2.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1680,"url":"https:\/\/learning.workfall.com\/learning\/blog\/create-a-no-code-graphql-server-using-hasura-and-postgresql\/","url_meta":{"origin":1518,"position":1},"title":"Create a No-code GraphQL Server Using Hasura and PostgreSQL","date":"March 28, 2023","format":false,"excerpt":"To handle CRUD, authorization, and business logic, backend developers frequently need to write several lines of code. All of this code must be tested, debugged, and maintained for the duration of the project. This consumes a significant amount of time that developers could be used to create new features. This\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"No-code GraphQL Server Using Hasura and PostgreSQL","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/03\/Cover-Images_Part2-1-3.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":617,"url":"https:\/\/learning.workfall.com\/learning\/blog\/aws-is-in-love-with-rust-and-you-should-be-too\/","url_meta":{"origin":1518,"position":2},"title":"AWS is in love with RUST and you should be too!","date":"November 11, 2021","format":false,"excerpt":"AWS is sponsoring the Rust programming language. And it is because it helps AWS write highly performant, safe infrastructure-level networking and other systems software. Why has Rust proved so popular at AWS? Let\u2019s discuss this in our blog! Rust has been voted Stack Overflow\u2019s most-loved programming language for five years\u2026","rel":"","context":"In &quot;AWS Cloud Computing&quot;","img":{"alt_text":"AWS SDK for Rust","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2021\/11\/Rust.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1568,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-build-a-rust-webassembly-frontend-app-with-yew-framework\/","url_meta":{"origin":1518,"position":3},"title":"How to Build a Rust WebAssembly Frontend App with Yew Framework?","date":"February 7, 2023","format":false,"excerpt":"While Rust is known for its backend web development capabilities, the introduction of WebAssembly (Wasm) has enabled the development of rich front-end apps in Rust. With the introduction of WebAssembly, it became possible to build frontend web apps in Rust, such as the one we just built, expanding development opportunities\u2026","rel":"","context":"In &quot;Frontend Development&quot;","img":{"alt_text":"Rust WebAssembly","src":"https:\/\/i1.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/02\/Cover-Images_Part2-1.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1661,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-handle-forms-efficiently-in-yew-web-development\/","url_meta":{"origin":1518,"position":4},"title":"How to Handle Forms Efficiently in Yew Web Development?","date":"March 7, 2023","format":false,"excerpt":"In order to create a Yew web application, one must create mechanisms to allow end users to interact with the system and provide data via online forms. This is where form handling comes into play. Yew offers Rust\u2019s rich type ecosystem which can be a great tool when it comes\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"Form Handling in Yew","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/03\/Cover-Images_Part2-1.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2266,"url":"https:\/\/learning.workfall.com\/learning\/blog\/deploying-a-rust-rocket-rest-api-on-aws-ec2-with-docker-and-github-actions\/","url_meta":{"origin":1518,"position":5},"title":"Deploying a Rust Rocket REST API on AWS EC2 with Docker and GitHub Actions","date":"May 16, 2023","format":false,"excerpt":"When Rust compiles code, you get an executable if you created the application using the --bin command. In this blog, we shall look at how we can create a Dockerfile to create an image with this executable. We shall then deploy this image on EC2 using GitHub Actions which will\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"Deploying a Rust Rocket REST API on AWS EC2 with Docker and GitHub Actions","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/05\/Cover-Images_Part2-1-1.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/1518"}],"collection":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/comments?post=1518"}],"version-history":[{"count":4,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/1518\/revisions"}],"predecessor-version":[{"id":2941,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/1518\/revisions\/2941"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/media\/1519"}],"wp:attachment":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/media?parent=1518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/categories?post=1518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/tags?post=1518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}