Logo

Programming-Idioms

History of Idiom 41 > diff from v67 to v68

Edit summary for version 68 by programming-idioms.org:
[Rust] Avoid link shortener if possible

Version 67

2019-12-08, 21:35:45

Version 68

2020-03-20, 17:57:00

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 = 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://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=507c4fe1ad34b7d9136792e8360e7c84