如何从STD::Borrow::CoW<Str>获取&字符串或字符串? [英] How do I get a &str or String from std::borrow::Cow<str>?

查看:0
本文介绍了如何从STD::Borrow::CoW<Str>获取&字符串或字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Cow

use std::borrow::Cow;  // Cow = clone on write
let example = Cow::from("def")

我希望将def从其中取出,以便将其追加到另一个String

let mut alphabet: String = "ab".to_string();
alphabet.push_str("c");
// here I would like to do:
alphabet.push_str(example);

这不起作用,我在Cow中找不到使&strString退出的适当方法。

推荐答案

将对example(即&example)的引用传递给push_str

let mut alphabet: String = "ab".to_string();
alphabet.push_str("c");  
alphabet.push_str(&example);

这之所以有效,是因为Cow实现了Deref

这篇关于如何从STD::Borrow::CoW<Str>获取&字符串或字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆