3 Frameworks for Building APIs Using Rust Tyler Charboneau April 30, 2019 Originally a personal project of Mozilla’s Graydon Hoare, Rust’s development began in 2006. It first said Hello World following its official announcement by Mozilla in 2010. Though Rust hasn’t enjoyed Python’s colossal growth, users love the language immensely. Rust topped Stack Overflow’s “most loved programming languages” ranking list in 2016, 2017, and 2018. We only expect it to grow more popular — especially since it’s continually being refined. We previously assessed the various pros and cons of Rust, and find it incredibly useful. The language shares many similarities with C and C++, while offering essential syntax modifications of its own. Accordingly, developers praise Rust for its open-source development and performance. This makes it a prime candidate for API creation. Below, we’ll review three superb frameworks for building APIs using Rust. 1. Rocket “Rocket is a web framework for Rust that makes it simple to write fast, secure web applications” Built using Rust’s core principles, Rocket is an excellent open-source framework for developers seeking a comprehensive toolset. Rocket includes a plethora of pre-packaged features, yet is quite extensible. Sergio Benitez publicly released Rocket in December of 2016, and remains a key contributor to date. Rocket doesn’t appear to have a bustling community via IRC, or stemming from its website. However, Rocket’s news blog helps developers keep abreast of ongoing changes. Accordingly, its development effort is strong. At the time of writing, 128 GitHub contributors have aided Rocket’s development since launch, and there have been over 1,250 commits. Having been updated 45 times following initial release, the current build of Rust is v0.4. On average, a new version of Rocket is released approximately every 18 days. Benefits As its namesake suggests, Rocket is built for speed with fast templating and incoming/outgoing data streaming, which is file-size agnostic The request-response process occurs in only three steps, emphasizing sound validation, customized error reporting, and simple arbitrary-handler functions Type-safe programming ensures requests follow successive, matching routes until validation does or doesn’t occur Form support is rich, and troubleshooting is simple. Bad requests don’t call your functions, and simple type changes help nail down faulty programming JSON support is baked in via Serialize or Deserialize. Using the FromData trait ensures easy value creation by type Request guards block your handlers from running if predetermined conditions aren’t met. Customizable ApiKey guards prevent unneeded sensitive operations, enforcing both type and API-header validations Drawbacks Rocket has depended on nightly Rust and doesn’t run on stable versions of the Rust compiler. This feature is pending, however – v0.5 is in development, and will add support for Rust stable Multipart form support is still a bit clunky, complicating file uploads and submissions. Again, improved support is slated for v0.5 It can be tricky keeping all components of Rocket properly updated over time Ongoing changes in Rust nightly can cause unpredictable, breaking changes 2. Actix-web “rust’s powerful actor system and most fun web framework” Built for speed and supportive of modern features, Actix-web is a framework focused on safe API creation. It was built to be usable and lightweight. Initially released in October of 2017 and maintained by Nikolay Kim, Actix-web supports the Actix actor framework. It has active Gitter and Reddit communities, and GitHub contributions are always welcomed. Actix-web is open source and has 108 contributors on GitHub. With over 2,500 commits and 89 releases, the framework is continually in development. Microsoft uses Actix-web in some applications, though additional details are murky. Benefits Supports both HTTP/1.x and HTTP 2.0 protocols Like Rocket, it supports data streaming while adding pipelining support, cutting down on response times by packaging requests on a single TCP connection Includes client support for asynchronous request handling, facilitating multiple concurrent executions without hiccups WebSockets support on both the client and server sides Developers can create customized request routes for easier resource access. This also allows for more consistent URI structuring Actix-web can serve both static and dynamic assets via OpenSSL or rustls One of the fastest-benchmarking Rust frameworks to date, with ample raw performance Drawbacks Heavy reliance on unsafe code instances to allow for improved performance, though this has been mitigated in recent versions Actix-web can be more complicated than Rocket to get started with, for example 3. Tower Web Tower Web is a fast web framework that aims to remove boilerplate. With a focus on simplifying API development via boilerplate removal, Tower Web is a lean-yet-powerful Rust framework. It’s built upon Tower, a complimentary library containing useful network components. Much like Actix-web leans on Actix, Tower Web does the same with Tower. It’s also built upon Tokio and Hyper – a non-blocking I/O platform and HTTP client, respectively. As a result, Tower Web is a well-rounded platform ready to tackle Rust API development. It’s also relatively new – Carl Lerche released Tower Web in August of 2018. He currently spearheads development efforts on GitHub, alongside 24 contributors. Since inception, it has been updated 11 times, approximately one release every 22 days. Its current version is v0.3.6. Benefits Since Tower Web is built upon Tower, Tokio, and Hyper, it has a solid foundation that offers promising future potential. As these components grow, Tower Web may reach feature parity with frameworks like Rocket Complete asynchronicity allows multiple processes to run simultaneously, eliminating bottlenecks HTTP is separated from your application’s logic, erasing the need for boilerplate and cutting down on cruft Tower Web is compatible with Rust stable, meaning ongoing changes to nightly won’t break your build Drawbacks Since Tower Web is newer, developers are still waiting for additional components, such as middleware, to be written Documentation could be more comprehensive Comparing Use Cases If you want to be on the bleeding edge and design web applications in compliance with emerging standards, Rocket is a great choice. That’s not saying the framework falters in other scenarios, but its current reliance on nightly Rust does raise stability concerns. Because Rocket isn’t asynchronous, it can only handle so many connections at any given time. Consequently, it may be better suited to smaller projects, or at least to web applications where active requests are minimized. However, Rocket is quite adept at handling large quantities of data. As the framework matures, it may very well power some large, high-profile projects. Rocket takes much of the guesswork out of the equation for newer users. Actix-web’s big advantages lie in two areas: actor management and speed. If you’re building out a web application based around accounts (email, web services with SOAP endpoints), the framework truly shines. This actor support is available in other frameworks like Rocket, though Actix-web’s implementation is more elegant. API builds reliant on the WebSockets protocol will mesh harmoniously with Actix-web. If you’re trying to squeeze as much performance out of your web servers as possible, the protocol’s low overhead will facilitate that. For developers looking to write the leanest code possible, Tower Web is a fantastic option. Boilerplate code has been placed on the chopping block, cutting down on production time. Code written within Tower Web has purpose, which can truly help with larger projects. It’s also straightforward to incorporate middleware into Tower Web – simplifying API creation and promoting a better experience for the end user. Fully asynchronous, the framework can also handle a massive amount of requests in parallel. Popular services built around numerous sessions will benefit significantly from this.