Rust 在这个例子中,str和String::from之间有什么区别?
在本例中,str和String::from之间有什么区别Rust 在这个例子中,str和String::from之间有什么区别?,rust,Rust,在本例中,str和String::from之间有什么区别 use std::borrow::Cow; fn main() { let s = "Hello world!"; let cow: Cow<str> = Cow::Owned(s); // type mismatch resolving `<str as std::borrow::ToOwned>::Owned == &str` //ex
use std::borrow::Cow;
fn main() {
let s = "Hello world!";
let cow: Cow<str> = Cow::Owned(s);
// type mismatch resolving `<str as std::borrow::ToOwned>::Owned == &str`
//expected struct `std::string::String`, found `&str`
// ok
let cow: Cow<str> = Cow::Owned(String::from(s));
}
将必须实现的泛型参数的关联类型作为输入。对于str。因此,s本身不能用于Cow::Owneds。将必须实现的泛型参数的关联类型作为输入。对于str。因此,s本身不能用于Cow::Owneds