The right mix of utilities can help you take your Rust app to the next level. Here are 19 of the most popular web utilities for Rust.

Rust is a programming language that is primarily focused on performance and safety. It is syntactically similar to C++ and extremely popular. In fact, Stack Overflow’s annual developer survey has named Rust the most-loved language among developers for five consecutive years.

What I love most about Rust is its static typing, which allows for the concept of null. Unlike other statically typed programming languages, Rust encodes the null concept using an optional type, which means the compiler requires you to handle the noncase error preventing the TypeError: Cannot read property 'foo' of null runtime error. Instead of the above runtime error, you get a compile-time error that can be resolved easily.

With Rust, you can also store your data on the stack or on the heap. This helps you determine what data is no longer needed during compile time.

There is a wide variety of open-source web utilities available to enhance Rust’s functionality. For example, although Rust’s standard library does not contain any regex parser/matcher, the regex utility provides a regex parser. With regex, you can search, split, or replace text.

Take a look at the expression below, which enables you to quickly search for a US number.

let re = Regex::new("[0-9]{3}-[0-9]{3}-[0-9]{4}").unwrap();
let mat = re.find("phone: 111-222-3333").unwrap();
assert_eq!((mat.start(), mat.end()), (7, 19));

This is just one example of how web utilities can help you take your Rust app to the next level. In this guide, we’ll introduce you to 19 of the most popular and useful web utilities for Rust:

  1. cookie
  2. clap
  3. heim
  4. tempfile
  5. urlencoded
  6. bodyparser
  7. queryst
  8. scgi
  9. sass-rs
  10. ratelimit_meter
  11. regex
  12. chrono
  13. time
  14. backtrace
  15. docopt
  16. structopt
  17. url
  18. robotparser
  19. multipart

1. cookie

This utility enhances HTTP cookie parsing and cookie jar management. The cookie library is licensed under MIT and Apache 2.0. According to crates.io, it has 62 versions, the stable most recent of which is 0.14.2. At the time of writing, cookie has been downloaded roughly 5.8 million times.

Features

The following features of cookie are disabled by default.

  • percent-encode allows percent encoding of the names and values in cookies. The cookie:: parse_encoded() method allows percent decoding for the name and value of a cookie while parsing. The cookie:: encoded method returns a wrapper around cookies and percent-encodes cookies with its Display implementation
  • signed enables cookies to be signed with the CookieJar::signed() method
  • private — When you have the private feature enabled with the CookieJar::private() method, cookies are encrypted, decrypted, and authenticated as they are added and retrieved
  • Key expansion automatically enables the signed and private features, as well as the Key::derive_from() method. With the Key::derive_from() method, you can derive a key structure that would be appropriate for use in the single or private jars, which would be shorter in length than a full key
  • secure is a meta feature that can toggle between enabling signed, private, and key-expansion. Enable it by adding the following code to your Cargo.toml file:
[dependencies.cookie]
features = ["secure", "percent-encode"]

2. clap

The clap crate is the best tool for parsing and validating command-line arguments and subcommands while writing console applications with Rust. This library is licensed with MIT and has 211 versions released, the most stable of which is 2.33.3 (version 3.0.0 is still in its beta phase). At the time of writing, it has more than 15 million downloads.

Alternatively, you could build your CLI using a YAML file to keep your Rust source tidy.

Features

The following built-in features are enabled by default.

  • Suggestions help users when they make typos by asking, for example, Did you mean '--myoption'?
  • Color — Error messages are more pronounced when they are colored. This feature turns on colored error messagea and functions only on non-Windows operating systems
  • Wrap help — With this feature, help in the terminal is wrapped instead of the 120 characters

To disable the features above, add the following code to your cargo.toml file.

[dependencies.clap]
version = "~2.27.0"
default-features = false

You can also enable only the features you would like to use by pasting the code below into your cargo.toml file.

[dependencies.clap]
version = "~2.27.0"
default-features = false
## Cherry-pick the features you'd like to use
features = [ "suggestions", "color" ]

The following are opt-in features; they are not built-in.

  • YAML enables you to build your CLI from a .yaml file
  • unstable enables you to use unstable clap features

3. heim

heim is a cross-platform library that can be used for system information fetching. It uses async-first and is cross-platform and modular.

The library’s GitHub page includes a helpful comparison of heim to other crates.

heim is licensed under MIT and Apache 2.0. The most stable of 15 versions is 0.0.10, while version 0.10 is in its beta phase.

4. tempfile

The tempfilecrate includes thetempfile()andtempdir() functions, which enable you to create temporary files and directories. Let’s say, for example, that you want to create a file but don’t care about its name since it will be a temporary file that you can delete later. The tempfile library would make this process this easier because it would delete the file once you’re done.

The tempfile() function relies on the operating system to remove the temporary file immediately after you close the last handle, while the tempdir() function relies on Rust destructors for cleanup. The tempdir() function may fail if destructors do not run, while the tempfile() function will hardly ever fail since it relies on the computer’s operating system.

This library is licensed under MIT and Apache 2.0 and has 35 versions released, its most recent stable version being 3.1.0.

5. urlencoded

urlencoded is a middleware for the Iron web framework that helps you decode URL data from GET and POST requests. It parses a URL query string into a HashMaps that maps string representations of keys onto a vec of string values. Values are stored in a vec to ensure that no information is lost if a key appears multiple times. The query string d=b&d=c results in a mapping from d to [b, c]. It parses POST request bodies for web form data (MIME type: application/x-www-form-urlencoded).

This library is licensed under MIT, has nine versions released (most stable version: 0.6.0), and requires the following dependencies.

  • bodyparser ^0.8
  • Iron >=0.5, <0.7
  • plugin ^0.2.6
  • url ^1.6

6. bodyparser

Part of Iron’s core middleware, bodyparser is, as its name suggests, a parsing middleware for Iron. It contains the following features:

  • raw — In Raw, bodyparser performs body parsing to string
  • json — The bodyparser framework can parse body into JSON
  • struct parses body into a struct using Serde

This library is licensed under MIT and has 15 versions. Its current, most stable version is 0.8.0, which requires the following dependencies:

  • Iron >=0.5, <0.7
  • Persistent ^0.4
  • plugin ^0.2
  • Serde ^1.0
  • serde_json ^1.0
  • serde_derive ^1.0

7. queryst

queryst is a Rust string parsing library that enables you to parse query strings to their corresponding JSON values.

Enable the queryst library by adding the following to your cargo.toml file.

[dependencies]
queryst = "1"

With queryst, the string foo[bar]=baz can be parsed to:

{
  "foo": {
    "bar": "baz"
   }
}

The URL-encoded string also works and can be parsed as:

parse('a%5Bb%5D=c');
// { "a": { "b": "c" } }

This library is licensed under MIT and has 21 versions published. Its most recent version, 2.0.2, requires the following dependencies.

  • Serde ^1
  • serde_json ^1
  • url ^1
  • lazy_static ^1 (optional)
  • regex ^0.2 (optional)

8. scgi

Simple Common Gateway Interface (SCGI) is a protocol for applications to interface with HTTP servers. scgi is an SCGI connector for Rust.

The library is licensed under MIT and Apache 2.0 and has 16 versions published. The most current version is 0.3.4, which requires bufstream ^0.1.1 as a dependency.

#rust #web-development #programming #developer

19 Awesome Web Utilities for Rust
1.95 GEEK