Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kdy1/guard_let
https://github.com/kdy1/guard_let
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/kdy1/guard_let
- Owner: kdy1
- Created: 2019-12-04T14:04:07.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2019-12-04T14:37:51.000Z (about 5 years ago)
- Last Synced: 2024-12-06T23:10:22.141Z (about 1 month ago)
- Language: Rust
- Size: 5.86 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# guard_let
[![doc.rs](https://docs.rs/guard_let/badge.svg)](https://docs.rs/guard_let)
[![crates.io](https://img.shields.io/crates/v/guard_let.svg)](https://crates.io/crates/guard_let)Guard let for rust. In rust, there exists a syntax sugar called if let.
But when it's nested, it make reading code really hard. `guard_let!` does inverse of lf let.
Execute code *below* guard only if guard matches,
and executes provided block if guard does not matches.# Usage
```rust
use guard_let::guard_let;
enum Enum {
A(String),
B(usize),
C(Struct),
}struct Struct {
foo: String,
}fn eat_string(_: String) {}
#[guard_let]
fn simple_ident() {
let v = Enum::A(String::from(""));guard_let!(v as Enum::A(s), {
// Type of v is Enum at here.
println!("v is not A: {:?}", v);
return;
});// Type of s is String
eat_string(s)
}#[guard_let]
fn pattern() {
let v = Enum::A(String::from(""));guard_let!(v as Enum::C(Struct { foo }), {
// Type of v is Enum at here.
println!("v is not C: {:?}", v);
return;
});// Type of s is String
eat_string(foo)
}```
# License
MIT