我有这个特点和简单的结构:
use std::path::{Path, PathBuf};
trait Foo {
type Item: AsRef<Path>;
type Iter: Iterator<Item = Self::Item>;
fn get(&self) -> Self::Iter;
}
struct Bar {
v: Vec<PathBuf>,
}
Run Code Online (Sandbox Code Playgroud)
我想实现以下Foo特征Bar:
impl Foo for Bar {
type Item = PathBuf;
type Iter = std::slice::Iter<PathBuf>;
fn get(&self) -> Self::Iter {
self.v.iter()
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了这个错误:
error[E0106]: missing lifetime specifier
--> src/main.rs:16:17
|
16 | type Iter = std::slice::Iter<PathBuf>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected lifetime parameter
Run Code Online (Sandbox Code Playgroud)
我发现无法在相关类型中指定生命周期.特别是我想表达迭代器不能超过self生命周期.
我如何修改Foo特征或Bar特征实现来使其工作?
我试图使用kcov来获取Rust库的代码覆盖率.我已经按照本教程构建和使用kcov.报道似乎有效,但我面临着一个奇怪的高覆盖率.项目中的某些文件获得100%的覆盖率,即使它们实际上根本没有被覆盖!
这是一个重现问题的最小项目:
Cargo.toml
[package]
name = "mypackage"
version = "0.1.0"
authors = ["mbrt"]
Run Code Online (Sandbox Code Playgroud)
SRC/lib.rs
pub mod subm;
pub fn coverage1(i : bool) -> bool {
if i {
true
}
else {
false
}
}
#[cfg(test)]
mod test {
use super::coverage1;
#[test]
fn test_coverage1() {
assert!(coverage1(true));
}
}
Run Code Online (Sandbox Code Playgroud)
SRC/subm.rs
pub fn coverage2(i : bool) -> bool {
if i {
true
}
else {
false
}
}
#[cfg(test)]
mod test {
#[test]
fn test_coverage2() {
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个使用两个线程的类:一个用于生产者,一个用于消费者.当前实现不使用锁:
#include <boost/lockfree/spsc_queue.hpp>
#include <atomic>
#include <thread>
using Queue =
boost::lockfree::spsc_queue<
int,
boost::lockfree::capacity<1024>>;
class Worker
{
public:
Worker() : working_(false), done_(false) {}
~Worker() {
done_ = true; // exit even if the work has not been completed
worker_.join();
}
void enqueue(int value) {
queue_.push(value);
if (!working_) {
working_ = true;
worker_ = std::thread([this]{ work(); });
}
}
void work() {
int value;
while (!done_ && queue_.pop(value)) {
std::cout << value << std::endl;
}
working_ = false;
}
private:
std::atomic<bool> working_; …Run Code Online (Sandbox Code Playgroud) 我在编写一个将字符串集合作为参数的函数时遇到了麻烦.我的功能看起来像这样:
type StrList<'a> = Vec<&'a str>;
fn my_func(list: &StrList) {
for s in list {
println!("{}", s);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我Vec<&'a str>按预期传递给函数,一切顺利.但是,如果我通过Vec<String>编译器抱怨:
error[E0308]: mismatched types
--> src/main.rs:13:13
|
13 | my_func(&v2);
| ^^^ expected &str, found struct `std::string::String`
|
= note: expected type `&std::vec::Vec<&str>`
= note: found type `&std::vec::Vec<std::string::String>`
Run Code Online (Sandbox Code Playgroud)
这是主要使用的:
fn main() {
let v1 = vec!["a", "b"];
let v2 = vec!["a".to_owned(), "b".to_owned()];
my_func(&v1);
my_func(&v2);
}
Run Code Online (Sandbox Code Playgroud)
我的函数无法获取自有字符串的向量.相反,如果我将StrList类型更改为:
type StrList = Vec<String>;
Run Code Online (Sandbox Code Playgroud)
第一次调用失败,第二次调用失败.
一种可能的解决方案是以这种方式生成一个Vec<&'a str> …
我在更改选项内部结构的字段时遇到问题.这是代码:
struct MyStruct {
field1 : i32,
field2 : i32,
// and many more...
}
impl MyStruct {
pub fn field1(&mut self, field1 : i32) -> &mut Self {
self.field1 = field1;
self
}
}
fn foo() -> Option<MyStruct> {
None
}
fn bar() -> Option<MyStruct> {
foo().as_mut().map(|s| s.field1(5))
}
fn main() {
bar();
}
Run Code Online (Sandbox Code Playgroud)
主要思想bar()是Option<MyStruct>从另一个函数返回一个Option<MyStruct>,更改该结构的字段(如果结果不是None)并返回结果Option<MyStruct>.
结构实现了构建器模式,所以我使用它.
在这种情况下,我收到以下错误:
test.rs:18:5: 18:40 error: mismatched types:
expected `core::option::Option<MyStruct>`,
found `core::option::Option<&mut MyStruct>`
(expected struct …Run Code Online (Sandbox Code Playgroud)