Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert from tuple (Option<&str>, Option<&str>) to tuple (Option<String>, Option<String>)?

I’m trying to get from this:

let a: Option<&str> = Some("foo");
let b: Option<String> = a.map(|s| s.to_string());

to a tuple form:

let c: (Option<&str>, Option<&str>) = (Some("bar"), Some("baz"));
let d: (Option<String>, Option<String>) = c.map(|s| (s.0.to_string(), s.1.to_string()));
7 |     let d: (Option<String>, Option<String>) = c.map(|s| (s.0.to_string(), s.1.to_string()));
  |                                                 ^^^ `(Option<&str>, Option<&str>)` is not an iterator
  |
  = note: the following trait bounds were not satisfied:
          `(Option<&str>, Option<&str>): Iterator`
          which is required by `&mut (Option<&str>, Option<&str>): Iterator

How can I convert the tuple of optional string slices to a tuple of optional strings using rust 1.61 or newer?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

playground link

>Solution :

Just do as you did when you had two variables:

let c = (Some("bar"), Some("baz"));
let d = (c.0.map(|s| s.to_string()), c.1.map(|s| s.to_string()));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading