o/option

Rust 中 Option 类型的基本移植

dev-master 2020-04-21 00:56 UTC

This package is auto-updated.

Last update: 2024-09-21 21:24:44 UTC


README

这是从 RustOption 类型的部分移植。大部分功能都包含了,但我跳过了一些在 PHP 环境中不太适用的 Rust 特定方法。

用法

基本用法与 Rust 相同。

use O\Option\{Some, None, OptionInterface};

// basic setting and getting of values
$greeting = new Some('hey there');
$name = new None();

echo $greeting->unwrap(); // echos 'hey there'
echo $name->unwrap(); // throws an OptionException
echo $name->unwrapOr('unknown'); // echos 'unknown'

function divide(int $x, int $y): OptionInterface
{
  if (y === 0) {
    return new None();
  }
  return new Some(x / y);
}

divide(1, 0); // None
divide(1, 1); // Some(1)

代码检查

$ composer lint

分析

$ composer analyze

测试

$ composer test:unit