Skip to content

Instantly share code, notes, and snippets.

@run-dlang
Created May 5, 2023 16:46
Show Gist options
  • Save run-dlang/316a1b9df1c14dade4c5ff13c927cc84 to your computer and use it in GitHub Desktop.
Save run-dlang/316a1b9df1c14dade4c5ff13c927cc84 to your computer and use it in GitHub Desktop.
Code shared from run.dlang.io. Run with '-unittest'
struct IndexOfResult
{
ptrdiff_t value;
alias value this;
bool opCast(T : bool)() const @nogc pure @safe
{
return value >= 0;
}
}
void main()
{
import std.string : indexOf;
import std.stdio : writeln, writefln;
enum chr = 'a';
enum arr = "dlang";
if(auto res = IndexOfResult(arr.indexOf(chr)))
{
assert(arr[res] == chr);
res.writefln!"[ --> %s ]";
}
else
writeln("Not found!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment