如何从元组转换(option<& str> option< st str; str>)tuple(option< string> string> option&lt string>)?

发布于 2025-02-13 06:51:02 字数 1042 浏览 1 评论 0原文

我正在尝试从中获取:

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

转到元组形式:

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

如何使用Rust 1.61或更新的元组转换为可选字符串的元组?

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?

playground link

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美胚控场 2025-02-20 06:51:02

就像您有两个变量时那样做:

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

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()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文