PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 14, 2022

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

 August 14, 2022     console, output, rust     No comments   

Issue

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>>
    where
        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)
        .into_iter()
        .map(|x| [&slice[..1], x.as_slice()].concat())
        .collect::<Vec<Vec<T>>>();

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


Solution

I solved the problem.

There was no running output because there was no print statement in the function itself, although it was there in the main function. Adding a print statement in the function itself solved the problem:]

let mut result = comb(&slice[1..], k - 1)
        .into_iter()
        .map(|x| {
               let output = [&slice[..1], x.as_slice()].concat();
               println!("{:?}", output);
               output
         })
        .collect::<Vec<Vec<T>>>();

Also, you would need to change the signature a little:

T: Copy + Debug

T would now need to implement Debug as well.



Answered By - Super User
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing