Function ctrs::id[][src]

pub fn id<T>(x: T) -> T

Identity is a unit under composition.

Overview

We describe a function over a generic type T that simply returns its parameterized value, unchanged. This might seem a little odd.

But as Bartosz describes, the motivation for understanding identity is to enable a higher order of composition:

You might be asking yourself the question: Why would anyone bother with the identity function — a function that does nothing? Then again, why do we bother with the number zero? Zero is a symbol for nothing. Ancient Romans had a number system without a zero and they were able to build excellent roads and aqueducts, some of which survive to this day. Neutral values like zero or id are extremely useful when working

with symbolic variables. That’s why Romans were not very good at algebra, whereas the Arabs and the Persians, who were familiar with the concept of zero, were. So the identity function becomes very handy as an argument to, or a return from, a higher-order function. Higher order functions are what make symbolic manipulation of functions possible. They are the algebra of functions.

To summarize: A category consists of objects and arrows (mor phisms). Arrows can be composed, and the composition is associative.

Every object has an identity arrow that serves as a unit under composition.

Example

use ctrs::id;

let x = 1;
assert_eq!(id(1), 1);

let y = "OK";
assert_eq!(id(y), "OK");