Logo

Programming-Idioms

History of Idiom 41 > diff from v63 to v64

Edit summary for version 64 by GlitchyCat:
[Rust] Added an explaination of the turbofish and an alternate version that doesn't use it

Version 63

2019-11-16, 18:55:09

Version 64

2019-12-06, 02:09:59

Idiom #41 Reverse a string

Create string t containing the same characters as string s, in reverse order.
Original string s must remain unaltered. Each character must be handled correctly regardless its number of bytes in memory.

Illustration

Idiom #41 Reverse a string

Create string t containing the same characters as string s, in reverse order.
Original string s must remain unaltered. Each character must be handled correctly regardless its number of bytes in memory.

Illustration
Code
let t = s.chars().rev().collect::<String>();
Code
let t: String = s.chars().rev().collect();
// or
let t = s.chars().rev().collect::<String>();
Comments bubble
collect is a function with a generic return type, so we must explicitly specify that we want a String back, either by annotating t's type as a String, or by specifying with the so-called "turbofish" syntax.
Demo URL
https://is.gd/Jztpr6
Demo URL
https://is.gd/Jztpr6