https://atcoder.jp/contests/abc237/tasks/abc237_b
typeを使うと型のaliasを作ることができます。
type Matrix<T> = Vec<Vec<T>>;
std::str::FromStrの逆のTraitはstd::string::ToStringなんですね。
// Matrix Transposition #![allow(non_snake_case)] fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_line(&mut line).ok(); line.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() } type Matrix<T> = Vec<Vec<T>>; fn read_matrix<T: std::str::FromStr>(H: usize) -> Matrix<T> { (0..H).map(|_| read_vec()).collect() } fn transpose<T: Copy>(M: &Matrix<T>) -> Matrix<T> { let H = M.len(); let W = M[0].len(); (0..W).map(|j| (0..H).map(|i| M[i][j]).collect()).collect() } fn print_matrix<T: std::string::ToString>(M: &Matrix<T>) { for v in M.iter() { println!("{}", v.iter().map(|c| c.to_string()). collect::<Vec::<String>>().join(" ")) } } fn main() { let v = read_vec(); let H: usize = v[0]; let _W: usize = v[1]; let A: Matrix<i32> = read_matrix(H); let B = transpose(&A); print_matrix(&B); }