Recap - Pretty Print Array
Part of the Fundamentals section of Coddy's Rust journey — lesson 59 of 75.
Challenge
EasyCreate 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
4Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 33Variables Part 2
Type DeclarationNaming ConventionsType InferenceRecap - Initialize VariablesType Casting9Loops
For Over SeriesWhile LoopBreakContinueNested LoopLoop LabelsInfinite LoopRecap - Dynamic Input12Arrays Basics
Declaring ArraysArray as ParameterAccessing ElementsModifying ArraysRecap - Pretty Print Array