Logo

Programming-Idioms

History of Idiom 41 > diff from v65 to v66

Edit summary for version 66 by programming-idioms.org:
[Rust] 2 ways => 2 impl

Version 65

2019-12-08, 21:32:48

Version 66

2019-12-08, 21:33:16

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: String = s.chars().rev().collect();
// or
let t = s.chars().rev().collect::<String>();
Code
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.
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