A Rust macro for quickly unwrapping a refutable pattern.

Overview

unwrap_let!

A Rust macro for quickly unwrapping a refutable pattern.

Install

Add unwrap_let to your dependency by running cargo add unwrap_let or editing Cargo.toml:

[dependencies]
unwrap_let = "0.1.0"

Then, import unwrap_let macro either by [macro_use] or a use statement.

// Option 1:
// Having this line in `lib.rs` or `main.rs` allows you to use `unwrap_let` in
// any file in your project without additional `use` statement.
#[macro_use]
extern crate unwrap_let;
// Option 2:
// Or, you can import it in each file that uses this macro.
use unwrap_let::unwrap_let;

Example

let val = Some(123);

unwrap_let!(Some(x) = val);
// `x` is defined
assert_eq!(x, 123);
enum Integer {
    Signed(i64),
    Unsigned(u64),
}

fn negate(unchecked_num: Integer) -> Integer {
    unwrap_let!(Integer::Signed(num) = unchecked_num, "expected a signed integer");
    Integer::Signed(-num)
}

Here is another example in our production test code.

fn test_multi_dimensional_array() {
    // ... parsing & lifting ...

    unwrap_let!(Some(TypeName::Array(dim1)) = variable_dimensional.type_name());
    unwrap_let!(TypeName::Array(dim2) = dim1.base());
    unwrap_let!(TypeName::Array(dim3) = dim2.base());
    unwrap_let!(TypeName::Array(dim4) = dim3.base());

    assert!(dim1.array_len() == Some(U256::from(3)));
    assert!(dim2.array_len() == Some(U256::from(2)));
    assert!(dim3.array_len() == None);
    assert!(dim4.array_len() == Some(U256::from(1)));

    // ... more checks ...
}

Minimum Supported Rust Version

unwrap_let internally uses "let-else" statement, which was introduced in Rust 1.65 (Nov 2022).

License

This project is licensed under the MIT license.

You might also like...
FastSSH is a TUI that allows you to quickly connect to your services by navigating through your SSH config.
FastSSH is a TUI that allows you to quickly connect to your services by navigating through your SSH config.

Connect quickly to your services 🚀 FastSSH is a TUI that allows you to quickly connect to your services by navigating through your SSH config. Instal

Jumpy is a tool that allows to quickly jump to one of the directory you've visited in the past

Jumpy Jumpy is a tool that allows to quickly jump to one of the directory you've visited in the past. It is heavily inspired by Zoxide but is more lig

A git command to quickly save your local changes in case of earthquake !

git-eq (aka git earthquake) Earthquakes are part of the daily life in many countries like in Taiwan. git-eq is a simple git command to quickly save yo

CLI tool to quickly create React + Typescript + Tailwind projects
CLI tool to quickly create React + Typescript + Tailwind projects

QUICK INIT CLI Tool to quickly create React projects with Tailwind automatically configured for use. Typescript is selected as default. If JS is to be

Quickly save and retrieve values for shell scripts.

Quickly save and retrieve values for shell scripts.

Introducing Inlyne, a GPU powered yet browsless tool to help you quickly view markdown files in the blink of an eye.
Introducing Inlyne, a GPU powered yet browsless tool to help you quickly view markdown files in the blink of an eye.

Inlyne - a GPU powered, browserless, markdown + html viewer inlyne README.md --theme dark/light About Markdown files are a wonderful tool to get forma

Shell Of A New Machine: Quickly configure new environments

Shell Of A New Machine soanm is a dead-simple tool for easily configuring new UNIX machines, with almost zero prerequisites on the target machine. All

Quickly find all blackhole directories with a huge amount of filesystem entries in a flat structure
Quickly find all blackhole directories with a huge amount of filesystem entries in a flat structure

findlargedir About Findlargedir is a tool specifically written to help quickly identify "black hole" directories on an any filesystem having more than

Tool that was built quickly for personal needs, you might find it useful though
Tool that was built quickly for personal needs, you might find it useful though

COPYCAT Produced with stable-diffusion Clipboard (copy/paste) history buffer for terminal emulators, MAC OS gui and VIM* environment usage. Rrequireme

Quickly build cool CLI apps in Rust.

QuiCLI Quickly build cool CLI apps in Rust. Getting started Read the Getting Started guide! Thanks This is only possible because of all the awesome li

Pascal Hertleif 538 Dec 5, 2022
A tool crate to quickly build rust command line application.

Falsework A tool crate to quickly build rust command line application.

Leon Ding 103 Dec 12, 2022
Rust command line utility to quickly display useful secrets in a Kubernetes namespace

kube-secrets This is a command line utility for quickly looking at secrets in a Kubernetes namespace that are typically looked at by humans. It specif

Frank Wiles 8 Feb 10, 2022
Js-macros - Quickly prototype Rust procedural macros using JavaScript or TypeScript!

js-macros Quickly prototype Rust procedural macros using JavaScript or TypeScript! Have you ever thought "this would be a great use case for a procedu

null 15 Jun 17, 2022
Cargo-eval - A cargo plugin to quickly evaluate some Rust source code.

cargo eval A cargo plugin to quickly evaluate some Rust source code. Installation $ cargo install --git https://github.com/timClicks/cargo-eval.git Us

Tim McNamara 9 Dec 21, 2022
Build Abstract Syntax Trees and tree-walking models quickly in Rust.

astmaker Build Abstract Syntax Trees and tree-walking models quickly in Rust. Example This example creates an AST for simple math expressions, and an

David Delassus 100 Jun 5, 2023
Base 32 + 64 encoding and decoding identifiers + bytes in rust, quickly

fast32 Base32 and base64 encoding in Rust. Primarily for integer (u64, u128) and UUID identifiers (behind feature uuid), as well as arbitrary byte arr

Chris Rogus 9 Dec 18, 2023
A CLI utility installed as "ansi" to quickly get ANSI escape sequences. Supports the most basic ones, like colors and styles as bold or italic.

'ansi' - a CLI utility to quickly get ANSI escape codes This Rust project called ansi-escape-sequences-cli provides an executable called ansi which ca

Philipp Schuster 5 Jul 28, 2022
Vim plugin to quickly parse strings into arrays.

butcher Vim plugin to quickly parse strings into arrays. It is painful to write arrays in any programming language, so butcher makes it easy for you.

null 5 Dec 31, 2021
Terminal UI that allows Alacritty users to quickly and easily shuffle through provided themes 🦄

Alac-pretty alac-pretty.mp4 If you're like me in that you constantly need to change the colors of your dev environment because visual stagnation reall

Benji Nguyen 17 Aug 29, 2022