an output range
the array to scan
the item to replace
the item to replace all instances of from with
the number of replacements
auto arr = [1, 2, 3, 4, 5]; auto from = [2, 3]; auto to = [4, 6]; auto sink = appender!(int[])(); size_t changed = 0; replaceInto(sink, arr, from, to, changed); assert(sink.data == [1, 4, 6, 4, 5]); assert(changed == 1);
Replace occurrences of from with to in subject and output the result into sink. changed counts how many replacements took place.