How to print the elements of a Vec<u32> in a single line seperated by white space?

I am a beginner programmer in Rust and have some background in python3. I want to print the elements of a Vec in a single line seperated by white spaces. I've tried using println!("{:?}", vector ) and also println!("{:#?}", vector) but these prints the elements comma seperated within square brackets. Is there any rust equivalance of the following python3 code print(*list)

You could start by trying to use print! instead of println! and a for loop.

1 Like
for el in &vector {
     print!("{} ", el);

With “use itertools::Itertools;” (using the popular itertools crate), you could do

println!("{}", vector.iter().format(" "));

Rust Playground

Or, if the element type only implements Debug, you can do

println!("{:?}", vector.iter().format(" "));

Rust Playground


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.