exercism.io rust track: Reverse String
June 7, 2021, – 100 days to offload countdown #93
As I said in my previuos post about the rust track of exercism.io, the curriculum was changed significantly since I last tackled it. The pythagorean triple exercise took some time – I solved the first project euler #9 task to find out that the task was changed considerably. And it was demoted to a side-exercise. The next main track exercise is a solution to string reversal. And that is kind of a let-down as a problem. My solution, after reading up on strings in rust, feels like cheating, but is the way I would implement it in any other language that gave me the same tools to work with:
pub fn reverse(input: &str) -> String { input.chars().rev().collect() }
This feels like cheating, because I don’t actually reverse the string, the language is, and something in me thinks the goal of this exercise would be the nitty gritty details of rust. But carefully reading the exercise and noticing that it may be my 13th exercise, it’s just the second one in the track, and designed to give you a feel of whats what. Seen this way, I think my solution is almost canonical.
But it has one flaw yet, it doesn’t handle unicode combined characters correctly, but the solution that does, is equaly almost writing itself:
use unicode_segmentation::UnicodeSegmentation; pub fn reverse(input: &str) -> String { input.graphemes(true).rev().collect() }
The exercise encourages you to search for external crates that can
help you to come up with a solution - it helped that the function is
called graphemes()
…