Rust Kata - Reversed Strings

By Lochard | As a newbie programmer | 19 Sep 2022


Learning from YayoDrayber, I am sharing my CodeWars Kata which could also be a reference for myself in the future.

 

DESCRIPTION:

Complete the solution so that it reverses the string passed into it.

'world'  =>  'dlrow'
'word'   =>  'drow'

Starting code

fn solution(phrase: &str) -> String {
    unimplemented!();
}

My attempt

fn solution(phrase: &str) -> String {
    phrase.chars().rev().collect::<String>()
}

Best Practice

fn solution(phrase: &str) -> String {
    phrase.chars().rev().collect()
}

Shorter Code

fn solution(phrase: &str) -> String {
    phrase.rsplit("").collect::<String>()
}

My postkata snippet

fn reverse_string(phrase: &str) -> String {
    phrase.rsplit("").collect()
}

 

How do you rate this article?

1


Lochard
Lochard

20240228 Arrived in the UK for about 2 week. All my fears persist. Some are even getting worse. https://github.com/locharp/asylum_diary/


As a newbie programmer
As a newbie programmer

Sharing entry level codes. My snippets on GitHub https://github.com/locharp/code-snippets

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.