- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在将适用于&str
的特征推广到其他字符串类型(例如Rc<str>
,Box<str>
和String
)时遇到一些问题。
首先,我的示例函数应适用于:
assert_eq!(count("ababbc", 'a'), 2); // already working
assert_eq!(count(Rc::from("ababbc"), 'a'), 2); // todo
assert_eq!(count("ababbc".to_string(), 'a'), 2); // todo
这是工作代码,它使第一次测试运行:
pub trait Atom: Copy + Eq + Ord + Display + Debug {}
impl Atom for char {}
pub trait Atoms<A, I>
where
I: Iterator<Item = A>,
A: Atom,
{
fn atoms(&self) -> I;
}
impl<'a> Atoms<char, std::str::Chars<'a>> for &'a str {
fn atoms(&self) -> std::str::Chars<'a> {
self.chars()
}
}
pub fn count<I, A, T>(pattern: T, item: A) -> usize
where
A: Atom,
I: Iterator<Item = A>,
T: Atoms<A, I>,
{
pattern.atoms().filter(|i| *i == item).count()
}
为了使下一个测试运行,我通过以下方式更改了
count
和
Atoms
的签名:
pub trait Atoms<'a, A, I>
where
I: Iterator<Item = A> + 'a,
A: Atom,
{
fn atoms<'b>(&'b self) -> I
where
'b: 'a;
}
impl<'a, S> Atoms<'a, char, std::str::Chars<'a>> for S
where
S: AsRef<str> + 'a,
{
fn atoms<'b>(&'b self) -> std::str::Chars<'b>
where
'b: 'a,
{
self.as_ref().chars()
}
}
但是现在函数计数不再编译了:
pub fn count<'a, I, A, T>(pattern: T, item: A) -> usize
where
A: Atom,
I: Iterator<Item = A> + 'a,
T: Atoms<'a, A, I>,
{
pattern.atoms().filter(|i| *i == item).count()
}
Playground-Link
the parameter type 'T' may not live long enough ... consider adding an explicit lifetime bound...: 'T: 'a'
。这对我来说不是完全可以理解的,因此我尝试应用了
T: Atoms<'a, A, I> + 'a
帮助。现在错误是:
'pattern' does not live long enough ... 'pattern' dropped here while still borrowed
。
Atoms
的情况下,而只是用
pattern.atoms();return 1;
替换了函数主体,因此我怀疑
Atoms
的类型签名不适合我的目的。
Atoms
或
count
的类型签名有什么问题?
最佳答案
trait Atoms<'a, A, I> where I: Iterator<Item = A> + 'a ...
通过编写此代码,您需要迭代器在生存期
'a
内使
超过。由于
'a
是特征的通用参数的一部分,因此它必须是可以延长使用期限的,然后才能开始(隐式)使用
<String as Atoms<'a, char, std::str::Chars>>::atoms
。这与返回新的迭代器对象的想法直接矛盾,因为该对象在调用
atoms()
之前不存在。
关于rust - 将特征实现从&T推广到AsRef <T>的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65625748/
我正在使用std::fs中的函数,这些函数带有path: impl AsRef之类的参数。我希望使自己的函数具有多态性,以便它们也可以采用任何impl AsRef而不是仅接受&str。但是,所讨论的类
如果我有一个包含这样的引用的结构: struct Struct { reference: &'a str } 如何为 Struct 实现 AsRef?我试过这个: impl AsRef> fo
我需要创建用于与 FFI 接口(interface)的 NewType 包装器。我想创建一个类似于 Rust 的 String 和 str 类型的接口(interface),所以我可以有一个拥有类型的
我有一个围绕 &str 的包装器来保持不变量。当我尝试返回一个新的包装器时,该包装器基本上将参数包装的相同数据包装到函数中( playground ),我收到“无法返回引用函数参数 s 的值” str
playground use std::path::Path; // fn f1(p: AsRef) { // println!("{}", p.as_ref().display()); //
我很难理解如何在下面的代码中使用生命周期。我知道明确的生命周期对于帮助编译器理解它何时可以保存/释放数据是必要的,但在这种特殊情况下, url.serialize() 生成一个匿名字符串,我不太确定如
我很难获得 AsRef以干净的方式工作。 const DEFAULT: &str = "lib"; use std::path::{Path, PathBuf}; fn extend(p: &Path
我正在尝试编写一个连接两个可迭代对象的函数,这两个可迭代对象的项目可以转换为 OsStr 引用,并且在尝试指定引用的生命周期时遇到了巨大的困难。 use std::convert::AsRef; us
以下代码显示了问题的来源。自 as_slice()已弃用,as_ref()建议作为替代品。 但是,在这种情况下使用它时,需要类型注释。 let s = "Hi"; // Thi
我在 Rust stable(截至目前,版本 1.2)中定义了一个 crate API,并且对定义我自己的类字符串类型的最佳实践感到困惑。 例如,我有一个包装字符串的 Foo 类型。 pub stru
这是我在我的 Rust 版本中看到的实现: #[stable(feature = "rust1", since = "1.0.0")] impl AsRef for PathBuf { #[i
我想写一个可以直接接受字符串切片的特征: use std::path::Path; trait Trait1 {} impl Trait1 for str {} // impl Trait1 for
Rust String 同时实现了 AsRef和 AsRef 但为什么 Rust 正确地允许 String 被借用为 &str 而不是 &[u8] 即使我们提供了显式类型注释提示? let s = S
AsRef 文档写道 Used to do a cheap reference-to-reference conversion. 我理解 reference-to-reference 部分 cheap
AsRef 有糖分吗?最好在稳定版。 我经常使用 as ref 声明类似字符串的参数,以便更轻松地与各种字符串(String、&str、&String 等)互操作: fn Fun>(my: S) {
AsRef 有糖分吗?最好在稳定版。 我经常使用 as ref 声明类似字符串的参数,以便更轻松地与各种字符串(String、&str、&String 等)互操作: fn Fun>(my: S) {
此代码无法编译: fn ref_on_int(_: T) where T: AsRef {} fn main() { ref_on_int(&0_i32) } 因为 the trait bou
我正在调用一个接受&[&str] 的函数。由于编写 ["aa", "bb"] 更方便,而不是 &["aa", "bb"],我决定添加 作为引用: struct Builder { s: Opt
我有一个将一些功能包装在切片周围的结构: use std::fmt::Debug; struct SliceWrapper { slice: &'a [T], pos: usize,
我有一个函数需要 AsRef作为参数,看起来像这样 fn test>(path: P) { path.join("13123123"); } 当我编译它时,它给了我以下错误 error[E05
我是一名优秀的程序员,十分优秀!