How to see running output from a Rust function on console?

I have a function in Rust which returns a vector of vectors of the number of combinations possible from a set given a number of digits for which the combinations are needed(nCk).

If the set is [1,2,3] and the number of digits(k) is 2, the output is:

[[1, 2], [1, 3], [2, 3]]

However, if I choose k to be a large number, it takes too much time and prints all of the output at once after it has finished processing.

Is it possible that it keeps giving me running output on the go?

In Java, I have the same thing and it gives me running output.

Here's my function:

fn comb<T>(slice: &[T], k: usize) -> Vec<Vec<T>>
        T: Copy,
    if k == 1 {
        return slice.iter().map(|x| vec![*x]).collect::<Vec<Vec<T>>>();

    if k == slice.len() {
        return vec![slice.to_vec()];

    let mut result = comb(&slice[1..], k - 1)
        .map(|x| [&slice[..1], x.as_slice()].concat())

    result.extend(comb(&slice[1..], k));
    return result;

Well, yes, you have no print statement within the comb function. Hence, it prints nothing.
You can insert a print statment in the map function, like:

let mut result = comb(&slice[1..], k - 1)
        .map(|x| {
               let sl = [&slice[..1], x.as_slice()].concat();
               println!("{:?}", sl);

1 Like

I have it in my main function and am using it like this:

fn main(){

    let a = vec![1, 2, 3];

   println!("{:?}", comb(&a, 2));


Yes, so?

Got it, thank you so much.

Also, the signature would change a little:

T: Copy + Debug

T would now need to implement Debug as well.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.