Menu
Coddy logo textTech

Recap - Pretty Print Array

Part of the Fundamentals section of Coddy's Rust journey — lesson 59 of 75.

challenge icon

Challenge

Easy

Create a program that receives an array of size 5, and prints the array beautifully in the following format:

[elem1, elem2, elem3, ...]

Each element is separated by , (comma followed by a space). There is no trailing comma or extra space before the closing bracket ].

For example, given the input 1,2,3,4,5, the output should be:

[1, 2, 3, 4, 5]

To iterate over elements in the array use:

for i in 0..arr.len() {
	arr[i];
	...
}

Try it yourself

use std::io;
use std::convert::TryInto;

fn main() {
    let mut input_str_arr = String::new();
    io::stdin().read_line(&mut input_str_arr).unwrap();
    let arr: [String; 5] = input_str_arr.split(',').map(String::from).collect::<Vec<String>>().try_into().unwrap();

    print!("[");
    // Write your code below

    
    println!("]");
}

All lessons in Fundamentals