相关疑难解决方法(0)

为什么我不能在同一个结构中存储值和对该值的引用?

我有一个值,我想在我自己的类型中存储该值以及对该值内部内容的引用:

struct Thing {
    count: u32,
}

struct Combined<'a>(Thing, &'a u32);

fn make_combined<'a>() -> Combined<'a> {
    let thing = Thing { count: 42 };

    Combined(thing, &thing.count)
}
Run Code Online (Sandbox Code Playgroud)

有时候,我有一个值,我想在同一个结构中存储该值和对该值的引用:

struct Combined<'a>(Thing, &'a Thing);

fn make_combined<'a>() -> Combined<'a> {
    let thing = Thing::new();

    Combined(thing, &thing)
}
Run Code Online (Sandbox Code Playgroud)

有时,我甚至没有参考该值,我得到同样的错误:

struct Combined<'a>(Parent, Child<'a>);

fn make_combined<'a>() -> Combined<'a> {
    let parent = Parent::new();
    let child = parent.child();

    Combined(parent, child)
}
Run Code Online (Sandbox Code Playgroud)

在每种情况下,我都会收到一个错误,即其中一个值"活不够长".这个错误是什么意思?

lifetime rust borrow-checker

193
推荐指数
3
解决办法
2万
查看次数

我用什么生命周期创建相互引用的Rust结构?

我想让结构成员知道他们的父母.这大约是我正在尝试做的事情:

struct Parent<'me> {
    children: Vec<Child<'me>>,
}

struct Child<'me> {
    parent: &'me Parent<'me>,
    i: i32,
}

fn main() {
    let mut p = Parent { children: vec![] };
    let c1 = Child { parent: &p, i: 1 };
    p.children.push(c1);
}
Run Code Online (Sandbox Code Playgroud)

我试图用生命周期来安抚编译器而不完全理解我在做什么.

这是我坚持的错误信息:

error[E0502]: cannot borrow `p.children` as mutable because `p` is also borrowed as immutable
  --> src/main.rs:13:5
   |
12 |     let c1 = Child { parent: &p, i: 1 };
   |                               - immutable borrow occurs here
13 |     p.children.push(c1);
   | …
Run Code Online (Sandbox Code Playgroud)

struct reference circular-reference rust

21
推荐指数
1
解决办法
5309
查看次数

何时以及如何使用引用向量

此代码正确编译.它有一些未使用的代码警告,但现在没问题.

use std::collections::BTreeMap;

enum Object<'a> {
    Str(String),
    Int(i32),
    Float(f32),
    Vector(Vec<&'a Object<'a>>),
    Prim(fn(State) -> State)
}

struct State<'a> {
    named: BTreeMap<String, &'a Object<'a>>,
    stack: Vec<Object<'a>>

}

impl<'a> State<'a> {
    fn push_int(&mut self, x: i32) {
        self.stack.push(Object::Int(x));
    }
}


fn main() {
    println!("Hello, world!");
    let obj = Object::Str("this is a test".to_string());
}
Run Code Online (Sandbox Code Playgroud)

这段代码的重要部分是push_intstack: Vec<Object<'a>>.

我正在尝试制作基于堆栈的VM.我想将状态传递给函数,函数可以从堆栈中取出东西,操作东西,然后将一些东西放回堆栈; 命名字段将保存命名对象.

我有一种预感,将堆栈表示为Vec<&'a Object<'a>>替代会更好.我现在拥有它的方式,我担心我会犯一些效率低下的错误.我的预感是正确的吗?

问题的第二部分是我不知道如何使引用版本的矢量工作.以适当的生命周期创建新值以推入堆栈对我来说不起作用.

我对这个问题有点模糊,所以如果我一直不清楚,请问我问题以清除问题.

reference vector rust

6
推荐指数
1
解决办法
3421
查看次数

借用Mutex借用参考和保护

我试图封装一些代码以避免重复它,涉及从Mutex中借用数据以及进一步的操作(我将这个问题留在了范围之外,但却是激励因素).

以下示例代码抱怨说guard活动时间不够长.但这正是为什么我要回归guard为此目的明确设计的结构.

这是借入检查员的限制吗?关于解决这个问题的任何建议?

use std::sync::{Mutex,MutexGuard};
use std::ops::DerefMut;

pub struct InnerData {
    count: i32  // sample only
}

pub struct Data {
    pub inner_data: Mutex<InnerData>
}

pub struct BorrowedInnerData<'a> {
    pub inner_data: &'a mut InnerData,
    guard: MutexGuard<'a,InnerData>,
}

impl Data {
    pub fn borrow_inner_data<'a>(&'a mut self) -> BorrowedInnerData<'a> {
        let guard = self.inner_data.lock().unwrap();
        BorrowedInnerData {
            inner_data: guard.deref_mut(),
            guard: guard,
        }
    }
}

fn main() {
    let mut data = Data {
        inner_data: Mutex::new( InnerData {
            count: 5 …
Run Code Online (Sandbox Code Playgroud)

lifetime rust

4
推荐指数
1
解决办法
1121
查看次数

在其他结构中存储struct的引用

我有两个结构.AppItem.

我想要实现的是通过将可变引用传递给s构造函数来在结构Itemitems向量中存储.AppItem

pub struct App<'a> {
    items: Vec<&'a Item>
}

impl<'a> App<'a> {
    pub fn new() -> App<'a> {
        App { items: Vec::new() }
    }
    pub fn register_item(&mut self, item: &'a Item) {
        self.items.push(item);
    }
}

pub struct Item;

impl Item {
    pub fn new(app: &mut App) -> Item {
        let item = Item;
        app.register_item(&item);
        item
    }
}

fn main() {
    let mut app = App::new();

    let item = Item::new(&mut …
Run Code Online (Sandbox Code Playgroud)

rust

3
推荐指数
1
解决办法
2834
查看次数

编译器表示,即使数据包含在Mutex中,也无法安全地在线程之间共享数据

我正在使用Rocket State,它传递给HTTP请求.此结构包含一个Mutex<DatastoreInstance>访问SQLite数据库的框架,并使用互斥锁锁定以使读写安全.

pub struct DatastoreInstance {
    conn: Connection,
}
Run Code Online (Sandbox Code Playgroud)

DatastoreInstance结构看起来像这样时,只有一个SQLite连接一切正常,但我还想在这个结构中添加一个事务对象:

pub struct DatastoreInstance {
    conn: Connection,
    events_transaction: Transaction,
}
Run Code Online (Sandbox Code Playgroud)

这没有编译,因为Transaction对象需要引用一个Connection应该具有它所知道的生命周期的对象.我正在使用的rusqlite中的ConnectionTransaction对象定义如下:

pub struct Connection {
    db: RefCell<InnerConnection>,
    cache: StatementCache,
    path: Option<PathBuf>,
}

pub struct Transaction<'conn> {
    conn: &'conn Connection,
    drop_behavior: DropBehavior,
}
Run Code Online (Sandbox Code Playgroud)

要解决生命周期问题,我必须添加这些生命周期参数才能使其正常工作:

pub struct DatastoreInstance<'a> {
    conn: Connection,
    events_transaction: Transaction<'a>,
}
Run Code Online (Sandbox Code Playgroud)

这是结果,并且应该根据我对生命周期和互斥体的理解而工作,但是我现在得到编译器错误告诉我:

`std::cell::RefCell<lru_cache::LruCache<std::string::String, rusqlite::raw_statement::RawStatement>>` cannot be shared between threads safely
    |                                                                                                            
    = help: within `rusqlite::Connection`, the trait …
Run Code Online (Sandbox Code Playgroud)

mutex thread-safety rust rust-rocket

3
推荐指数
1
解决办法
905
查看次数