2023-02-09 00:25:01 +08:00
|
|
|
/*
|
2023-02-05 16:25:42 +08:00
|
|
|
* File: print_util.rs
|
|
|
|
* Created Time: 2023-02-05
|
|
|
|
* Author: sjinzh (sjinzh@gmail.com)
|
2023-02-09 00:25:01 +08:00
|
|
|
*/
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
|
|
use std::fmt::Display;
|
2023-02-09 00:25:01 +08:00
|
|
|
use std::collections::{HashMap, LinkedList};
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
|
|
/* Print an array */
|
|
|
|
pub fn print_array<T: Display>(nums: &[T]) {
|
|
|
|
print!("[");
|
|
|
|
if nums.len() > 0 {
|
|
|
|
for (i, num) in nums.iter().enumerate() {
|
|
|
|
print!("{}{}", num, if i == nums.len() - 1 {"]"} else {", "} );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print!("]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print a hash map */
|
|
|
|
pub fn print_hash_map<TKey: Display, TValue: Display>(map: &HashMap<TKey, TValue>) {
|
|
|
|
for (key, value) in map {
|
|
|
|
println!("{key} -> {value}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print a queue or deque */
|
|
|
|
pub fn print_queue<T: Display>(queue: &LinkedList<T>) {
|
|
|
|
print!("[");
|
|
|
|
let iter = queue.iter();
|
|
|
|
for (i, data) in iter.enumerate() {
|
|
|
|
print!("{}{}", data, if i == queue.len() - 1 {"]"} else {", "} );
|
|
|
|
}
|
|
|
|
}
|