fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1 LL| |// compile-flags: -Zinline-mir 2 LL| | 3 LL| |use std::fmt::Display; 4 LL| | 5 LL| 1|fn main() { 6 LL| 1| permutations(&['a', 'b', 'c']); 7 LL| 1|} 8 LL| | 9 LL| |#[inline(always)] 10 LL| 1|fn permutations<T: Copy + Display>(xs: &[T]) { 11 LL| 1| let mut ys = xs.to_owned(); 12 LL| 1| permutate(&mut ys, 0); 13 LL| 1|} 14 LL| | 15 LL| 16|fn permutate<T: Copy + Display>(xs: &mut [T], k: usize) { 16 LL| 16| let n = length(xs); 17 LL| 16| if k == n { 18 LL| 6| display(xs); 19 LL| 10| } else if k < n { 20 LL| 15| for i in k..n { 21 ^10 22 LL| 15| swap(xs, i, k); 23 LL| 15| permutate(xs, k + 1); 24 LL| 15| swap(xs, i, k); 25 LL| 15| } 26 LL| 0| } else { 27 LL| 0| error(); 28 LL| 0| } 29 LL| 16|} 30 LL| | 31 LL| 16|fn length<T>(xs: &[T]) -> usize { 32 LL| 16| xs.len() 33 LL| 16|} 34 LL| | 35 LL| |#[inline] 36 LL| 30|fn swap<T: Copy>(xs: &mut [T], i: usize, j: usize) { 37 LL| 30| let t = xs[i]; 38 LL| 30| xs[i] = xs[j]; 39 LL| 30| xs[j] = t; 40 LL| 30|} 41 LL| | 42 LL| 6|fn display<T: Display>(xs: &[T]) { 43 LL| 24| for x in xs { 44 ^18 45 LL| 18| print!("{}", x); 46 LL| 18| } 47 LL| 6| println!(); 48 LL| 6|} 49 LL| | 50 LL| |#[inline(always)] 51 LL| 0|fn error() { 52 LL| 0| panic!("error"); 53 LL| 0|} 54