简体   繁体   English

使用 Combine 的 Rust 所有权和生命周期

[英]Rust Ownership and Lifetimes using Combine

I've read the docs on ownership and lifetimes and I think I understand them but I'm having trouble with a specific bit of code.我已经阅读了关于所有权和生命周期的文档,我想我理解它们,但我在处理特定代码时遇到了麻烦。

I have a struct called Branch like this:我有一个名为Branch的结构,如下所示:

struct Branch {
    slot: u8,
    yaw: u8,
    pitch: u8,
    length: u8
}

I'm using the combine library (it's a parser combinator) to parse a string into a Branch.我正在使用combine库(它是一个解析器组合器)将一个字符串解析为一个分支。 The parsers look like this:解析器如下所示:

let hex_re = Regex:new(r"[0-9a-fA-F]").unwrap();
let hex = || find(&hex_re).map(|s| u8::from_str_radix(s, 16));
let branch = |length: u8| {
    (hex(), hex(), hex())
        .map( |t| (t.0.unwrap(), t.1.unwrap(), t.2.unwrap()) )
        .map( |(slot,yaw,pitch)| Branch { slot, yaw, pitch, length } )
}

The parsers are fairly simple, the first hex takes a regular-expression which matches a single hexidecimal character and maps it into a u8.解析器相当简单,第一个hex采用匹配单个十六进制字符的正则表达式并将其映射到 u8。 The second branch maps 3 hex characters into a Branch eg 3d2.第二个branch将 3 个十六进制字符映射到一个分支,例如 3d2。

The problem arises when I call the parser branch(1).parse("3d2") , the compiler reports an error 'length' does not live long enough .当我调用解析器branch(1).parse("3d2") ,问题出现了,编译器报告错误'length' does not live long enough I think I understand this error, if I'm not mistaken it's because length goes out of scope when the closure completes and so the length variable is deallocated even though it is still being used by the newly created Branch.我想我理解这个错误,如果我没有弄错的话,那是因为当闭包完成时length超出了范围,因此即使新创建的分支仍在使用length变量,它也会被释放。

So, I tried to get around this by converting length: u8 to length: &u8 like so:所以,我试图通过将length: u8转换为length: &u8来解决这个问题:

let branch = |len: &u8| {
    (hex(), hex(), hex())
        .map( |t| (t.0.unwrap(), t.1.unwrap(), t.2.unwrap()) )
        .map( |(slot,yaw,pitch)| Branch { slot, yaw, pitch, length: *len } )
};

// calling the parser
branch(&(1 as u8)).parse("3d2");

But that results in this error:但这会导致此错误:

type of expression contains references that are not valid during the expression: `combine::combinator::Map<combine::combinator::Map<(combine::combinator::Map<combine::regex::Find<&regex::Regex, &str>, [closure@src\lsystem.rs:26:37: 26:70]>, combine::combinator::Map<combine::regex::Find<&regex::Regex, &str>, [closure@src\lsystem.rs:26:37: 26:70]>, combine::combinator::Map<combine::regex::Find<&regex::Regex, &str>, [closure@src\lsystem.rs:26:37: 26:70]>, combine::combinator::Map<combine::regex::Find<&regex::Regex, &str>, [closure@src\lsystem.rs:26:37: 26:70]>), [closure@src\lsystem.rs:30:19: 30:65]>, [closure@src\lsystem.rs:31:19: 31:80 length:&&u8]>`

I have no idea what this error is about.我不知道这个错误是关于什么的。 Any help would be much appreciated.任何帮助将非常感激。

This solved it:这解决了它:

let hex_re = Regex:new(r"[0-9a-fA-F]").unwrap();
let hex = || find(&hex_re).map(|s| u8::from_str_radix(s, 16));
let branch = |length: u8| {
    (hex(), hex(), hex())
        .map( |t| (t.0.unwrap(), t.1.unwrap(), t.2.unwrap()) )
        .map( move |(slot,yaw,pitch)| Branch { slot, yaw, pitch, length } )
}

placing move on the second .map works.将移动放在第二个 .map 作品上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM