extern crate permutate; // 0.3.2
use permutate::Permutator;
use std::io::{self, Write};
fn main() {
let stdout = io::stdout();
let mut stdout = stdout.lock();
let list: &[&str] = &["one", "two", "three", "four"];
let list = [list];
let mut permutator = Permutator::new(&list[..]);
if let Some(mut permutation) = permutator.next() {
for element in &permutation {
let _ = stdout.write(element.as_bytes());
}
let _ = stdout.write(b"\n");
while permutator.next_with_buffer(&mut permutation) {
for element in &permutation {
let _ = stdout.write(element.as_bytes());
}
let _ = stdout.write(b"\n");
}
}
}
2条答案
按热度按时间w8f9ii691#
你可以使用
IteratorRandom
来得到一个更短的解,这是迭代器的一个扩展特性,它提供了choose_multiple
和choose_multiple_fill
这样方便的函数:2hh7jdfx2#
听起来像是可以使用permutate机箱中的排列: