对于关联数组,我们可以编写
if( elem in array) { .. }
Run Code Online (Sandbox Code Playgroud)
我们为一个简单的数组写了什么?我想写验证,例如
enforce(input in [10,20,40]);
Run Code Online (Sandbox Code Playgroud)
Nil*_*Nil 21
in遗憾的是在阵列上不起作用.您必须在http://dlang.org/phobos/std_algorithm.html中使用canFind或search定义.因为你只想知道它是否存在,而不是它的位置,是正确的工具.std.algorithm canFind
import std.algorithm: canFind;
if (my_array.canFind(42)) { stuff }
Run Code Online (Sandbox Code Playgroud)