rust - 无法模拟返回 Option<&String> 的特征
问题描述
我正在尝试使用mockall
板条箱模拟一个特征:
#[automock]
trait Foo {
fn foo(input: &Vec<String>) -> Option<&String>;
}
但是,我收到以下错误:
error[E0637]: `&` without an explicit lifetime name cannot be used here
--> src/names_matcher.rs:79:51
|
79 | fn foo(input: &Vec<String>) -> Option<&String>;
| ^ explicit lifetime name needed here
error[E0623]: lifetime mismatch
--> src/names_matcher.rs:77:9
|
77 | #[automock]
| ^^^^^^^^^^^
| |
| ...but data from `input` is returned here
78 | trait Foo {
79 | fn foo(input: &Vec<String>) -> Option<&String>;
| ------------ this parameter and the return type are declared with different lifetimes...
我要实现的函数将返回 None 或 Some 并引用输入中向量的元素之一。如果我尝试在考虑到这一点的情况下定义生命周期:
#[automock]
trait Foo {
fn foo<'r>(input: &'r Vec<String>) -> Option<&'r String>;
}
我得到以下信息:
error[E0261]: use of undeclared lifetime name `'r`
--> src/names_matcher.rs:79:20
|
79 | fn foo<'r>(input: &'r Vec<String>) -> Option<&'r String>;
| ^^ undeclared lifetime
|
help: consider introducing lifetime `'r` here
|
77 | #[automock]<'r>
| ^^^^
help: consider introducing lifetime `'r` here
|
77 | 'r, #[automock]
| ^^^
但是这些建议都不起作用,它们会产生语法错误。有没有办法像我上面定义的那样模拟一个特征?
解决方案
推荐阅读
- linux - 如果内部没有描述或记录,为什么浮点比较有效?这是gdb的怪癖吗?
- c# - 如何在 Unity 中启用和禁用多个游戏对象?
- linux - 带 -y 选项的 ldapsearch 抛出“无效凭据”错误
- oracle - PL/SQL: ORA-00933: SQL 命令未在游标 SQL 开发人员中正确结束
- javascript - 查找数组的值?
- java - 怎么修?变量可能尚未初始化
- java - 为什么这段代码没有退出 while 循环?绝对文件路径或名称有问题吗?
- scala - Spark - 将数据帧分成n条记录
- r - 将 JSON 文件转换为 SQLite 文件
- spring - Spring HATEOAS 不会在响应中生成 _embedded 和 _link 属性