{"id":23745267,"url":"https://github.com/ilyvion/ward","last_synced_at":"2025-12-12T13:41:29.676Z","repository":{"id":57671861,"uuid":"207351589","full_name":"ilyvion/ward","owner":"ilyvion","description":"Provides a ward! macro which returns the contents of an Option\u003cT\u003e and otherwise returns early, and a guard! macro, which does the same, but with a syntax more similar to Swift's guard syntax","archived":false,"fork":false,"pushed_at":"2019-09-16T11:34:06.000Z","size":23,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-23T08:36:00.468Z","etag":null,"topics":["guard","rust"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ilyvion.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-09T16:15:03.000Z","updated_at":"2023-03-28T20:10:50.000Z","dependencies_parsed_at":"2022-08-31T01:22:12.901Z","dependency_job_id":null,"html_url":"https://github.com/ilyvion/ward","commit_stats":null,"previous_names":["ilyvion/ward","alexschrod/ward"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilyvion%2Fward","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilyvion%2Fward/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilyvion%2Fward/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilyvion%2Fward/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ilyvion","download_url":"https://codeload.github.com/ilyvion/ward/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231987766,"owners_count":18456475,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["guard","rust"],"created_at":"2024-12-31T12:56:56.595Z","updated_at":"2025-12-12T13:41:24.640Z","avatar_url":"https://github.com/ilyvion.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ward\n\n[![Crates.io](https://img.shields.io/crates/v/ward)](https://crates.io/crates/ward)\n[![Crates.io](https://img.shields.io/crates/l/ward)](https://crates.io/crates/ward)\n[![Crates.io](https://img.shields.io/crates/d/ward)](https://crates.io/crates/ward)\n[![Docs.io](https://docs.rs/ward/badge.svg)](https://docs.rs/ward)\n![no_std](https://img.shields.io/badge/no__std-yes-brightgreen)\n\nThis crate exports two macros, which are intended to replicate the functionality of Swift's\nguard expression with `Option\u003cT\u003e`.\n\nThe `guard!` macro was created to emulate the `guard let` statement in Swift. This macro is only\nreally useful for moving values out of `Option\u003cT\u003e`s into variables.\nThe `ward!` macro, on the other hand, doesn't force the creation of a variable, it only returns\nthe value that the `guard!` variable would place into a variable. As such, it's a more flexible\nversion of the `guard!` macro; and probably also somewhat more Rustic.\n\n## Examples\n\n```rust\nlet sut = Some(\"test\");\n\n// This creates the variable res, which from an Option\u003cT\u003e will return a T if it is Some(T), and will\n// otherwise return early from the function.\nguard!(let res = sut);\nassert_eq!(res, \"test\");\n```\n\nThe `ward!` macro, by comparison, just returns the value, without forcing you to make a variable\nfrom it (although we still do in this example):\n\n```rust\nlet sut = Some(\"test\");\nlet res = ward!(sut);\nassert_eq!(res, \"test\");\n```\n\nBoth macros also support an `else` branch, which will run if the `Option\u003cT\u003e` is `None`:\n\n```rust\nlet sut = None;\nguard!(let _res = sut, else {\n    println!(\"This will be called!\");\n\n    // Because sut is None, the else branch will be run. When the else branch is invoked, guard!\n    // no longer automatically returns early for you, so you must do so yourself if you want it.\n    return;\n});\nunreachable!();\n```\n\nBoth macros also support an alternative \"early return statement\", which will let you e.g.\n`break` within loops:\n\n```rust\n// Not that you couldn't (and probably should) do this case with `while let Some(res) = sut`...\nlet mut sut = Some(0);\nloop {\n    let res = ward!(sut, break);\n    sut = if res \u003c 5 {\n        Some(res + 1)\n    } else {\n        None\n    }\n}\nassert_eq!(sut, None);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Filyvion%2Fward","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Filyvion%2Fward","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Filyvion%2Fward/lists"}