Is rust verbose or it is me?
Recently I tried axum, it is an amazing framework. But I have a feeling that I have to type more than I should. I think that maybe the verbosity comes from the rust language itself. What do you think?
Let's take 2 example:
Problem
Reverse every character in each word of a sentence. Input: I'm having a feeling that rust is a little verbose. Output: m'I gnivah a gnileef taht tsur si a elttil esobrev
Rust code
pub fn reverse_words(s: String) -> String {
s.split_whitespace()
.map(|s| s.chars().rev().collect::<String>())
.intersperse(String::from(" "))
.collect()
}
Kotlin equivalent
fun reverseWords(s: String): String {
return s.split(' ').map { it.reversed() }.joinToString(" ")
}
Problem
Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string. Input: s=abcdefg; k=2 . Output bacdfeg
Rust code
pub fn reverse_str(s: String, k: usize) -> String {
let data = s.as_bytes()
.chunks(k)
.enumerate()
.map(|(i, arr)| {
if i%2 == 0 {
arr.to_vec().into_iter().rev().collect()
} else {
arr.to_vec()
}
})
.flatten()
.collect();
String::from_utf8(data).unwrap()
}
Kotlin equivalent
fun reverseStr(s: String, k: Int): String {
return s.chunked(k)
.withIndex()
.map { (i, arr) -> if(i%2==0) arr.reversed() else arr }
.joinToString("")
}