Skip to main content

morph

Macro morph 

Source
macro_rules! morph {
    ($t:expr, |$v:ident| $e:expr $(,)?) => { ... };
    ($t:expr, |$v:ident| $e:expr, $($rest:expr),+ $(,)?) => { ... };
    ($t:expr; |$v:ident| $e:expr $(;)?) => { ... };
    ($t:expr; |$v:ident| $e:expr; $($rest:expr);+ $(;)?) => { ... };
}
Expand description

βŒ— πŸ’± Morphs a value through one or more transformation steps and returns the result.


πŸ“ code/result


morph! expresses a left-to-right transformation pipeline without explicit intermediate bindings in user code.

Conceptually, morph!(x, a, b, c) corresponds to c(b(a(x))).

A single binding introduces the pipeline variable, which is reused implicitly by subsequent steps. Each step may change the value’s type.

This macro is const-capable and does not create closures. It is the trait-free mechanical form of Morph.

Steps can be written as:

  • a comma-separated list of expressions, or
  • a semicolon-separated list of expressions.

Β§Examples

const S: &str = morph!(3u8, |v| v as usize, v * 2, v + 1, if v == 7 { "7" } else { "not7" });
const_assert![eq_str S, "7"];

For pipelines that preserve value identity and mutate in place, see hook!.