From 04ff4a12fe09f444aa389b9084564bba1387e905 Mon Sep 17 00:00:00 2001 From: Ben Frankel Date: Wed, 31 Jul 2024 20:51:12 -0700 Subject: [PATCH] Write up README.md --- README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d30d158..20a1e63 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,30 @@ [![Docs](https://docs.rs/tiny_bail/badge.svg)](https://docs.rs/tiny_bail/latest/tiny_bail/) [![License](https://img.shields.io/badge/license-MIT%2FApache-blue.svg)](https://github.com/benfrankel/tiny_bail) -TODO: Sample code. +This crate provides four failure-skipping macros: `r!`, `rq!`, `c!`, and `cq!`; along with their long-form aliases +`or_return!`, `or_return_quiet!`, `or_continue!`, and `or_continue_quiet!`, respectively. + +The macros support handle both `Option` and `Result` types out-of-the-box. This can be extended by implementing the +`Success` trait for other types. + +You can provide a return value as an optional first argument to the macro, or you can omit it to default to +`Default::default()`—which also works in functions with no return value. + +# Example ```rust +/// Increment the last number of a list, or warn if it's empty. +fn increment_last(list: &mut [usize]) { + // With `r!`: + *r!(list.last_mut()) += 1; + + // Without `r!`: + if let Some(x) = list.last_mut() { + *x += 1; + } else { + warn!("Empty list"); + } +} ``` # License