Beginner question. I have just finished reading the first few chapters of the book and trying out some simple problems.
This program generates all combinations of n dice rolls, and prints their totals. For example, for 3 dice rolls on a dice with sides 1, 2, 3, and 4, this is the output:
[1, 1, 1] - total = 3
[1, 1, 2] - total = 4
[1, 1, 3] - total = 5
[1, 1, 4] - total = 6
[1, 2, 1] - total = 4
...
[4, 4, 1] - total = 9
[4, 4, 2] - total = 10
[4, 4, 3] - total = 11
[4, 4, 4] - total = 12
This is the program:
const DICE: [i32; 4] = [1, 2, 3, 4]; // Your dice sides from smallest to largest
pub fn start() {
let rolls = 3;
combos(rolls, 0, Vec::new());
}
// rolls - the number of times the dice is rolled
// current - the current roll index (keeps track of number of rolls)
// output - the vector for storing the outcome of each roll
fn combos(rolls: i32, current: i32, mut output: Vec<i32>) {
if current >= rolls {
let sum: i32 = output.iter().sum();
println!("{:?} - total = {sum}", output);
return;
}
for n in 1..=DICE[DICE.len() - 1] {
output.push(n);
combos(rolls, current + 1, output.clone());
output.pop();
}
}
What I am trying to do is store those sums in a vector rather than just printing them. A global vector where I could push the sums would do the job, but it's probably not the right approach. I am also pretty sure that cloning a vector for every recursive call is probably not a good idea either. Is there a way to avoid this?
Thanks