- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已阅读有关所有权和生命周期的文档,我想我理解它们,但我在处理特定代码时遇到了问题。
我有一个名为 Branch
的结构,如下所示:
struct Branch {
slot: u8,
yaw: u8,
pitch: u8,
length: u8
}
我正在使用 combine
库(它是一个解析器组合器)将字符串解析为分支。解析器看起来像这样:
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 } )
}
解析器相当简单,第一个 hex
采用匹配单个十六进制字符的正则表达式并将其映射到 u8。第二个 branch
将 3 个十六进制字符映射到 Branch,例如3d2.
当我调用解析器branch(1).parse("3d2")
时出现问题,编译器报错'length' does not live long enough
.我想我理解这个错误,如果我没记错的话,那是因为 length
在闭包完成时超出了范围,所以 length
变量被释放,即使它仍然是被新创建的 Branch 使用。
所以,我试图通过将 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");
但这会导致这个错误:
type of expression contains references that are not valid during the expression: `combine::combinator::Map<combine::combinator::Map<(combine::combinator::Map<combine::regex::Find<®ex::Regex, &str>, [closure@src\lsystem.rs:26:37: 26:70]>, combine::combinator::Map<combine::regex::Find<®ex::Regex, &str>, [closure@src\lsystem.rs:26:37: 26:70]>, combine::combinator::Map<combine::regex::Find<®ex::Regex, &str>, [closure@src\lsystem.rs:26:37: 26:70]>, combine::combinator::Map<combine::regex::Find<®ex::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]>`
我不知道这个错误是关于什么的。任何帮助将不胜感激。
最佳答案
这解决了它:
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 } )
}
将 move 放在第二个 .map 上有效。
关于使用 Combine 的 Rust 所有权和生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48707861/
我想使用 Haskell 的 parsec 库来实现这个语法规则: ((a | b | c)* (a | b))? 这是一个接受可选(即可能为空)字符串的解析器规则。如果它接受的字符串不为空,则可以通
Python 的 itertools.combinations() 创建的结果是数字的组合。例如: a = [7, 5, 5, 4] b = list(itertools.combinations(a
I found a good script for the permutation of lists, combining and not combining list position on
我正在使用 Beam 管道计算流式数据的电话号码频率。我使用的滑动窗口每 5 分钟重复一次,总周期为 15 分钟,因此正如预期的那样,对于某些输入,当输入落在多个窗口中时,我会得到多个输出。 计算出现
这个问题已经有答案了: Pandas Merging 101 (8 个回答) 已关闭 3 年前。 我有两个数据帧,我想对其执行外连接。两个数据框共享一个公共(public)索引名称以及多个也共享相同名
我在谷歌上搜索了很多天这个问题,但一无所获。我需要做一个 SELECT,DUPLICATE 和 DUPLICATE 和 DUPLICATE 取决于用户。之后,我需要将每个选项的值组合到我选择的一个选择
这个问题在这里已经有了答案: Java 8 Streams: multiple filters vs. complex condition (4 个答案) 关闭 4 年前。 需要过滤所有适合其领域某
运行 cv2.getRectSubPix(img, (5,5), (0,0)) 抛出错误: OpenCV Error: Unsupported format or combination of for
没有重复的组合看起来像这样,当可供选择的元素数 (n) 为 5 且选择的元素数 (r) 为 3 时: 0 1 2 0 1 3 0 1 4 0 2 3 0 2 4 0 3 4 1 2 3 1 2 4 1
我在学校的数学一直不太好,我意识到我实际上需要与 pow(base, exponent) 函数相反的函数,该函数对某个数字进行乘方运算,例如 2 ^ 4 = 16 搜索答案我发现对数 log() 应该
我确信这很简单,但我很难找到一种方法来做到这一点。基本上,如果我有一个包含 P 列和 V^P 行的数组,我如何填写所有组合,即基本上所有可能的数字以 P 数字的 V 为基数。例如,对于 P=3 和 V
我想知道一种可能的算法来计算所有可能的组合,没有重复,从 length=1 开始,直到 length=N 的 N 个元素。 例子: 元素:1、2、3。 输出: 1 2 3 12 13 23 123 最
使用三种不同颜色的颜料可以用多少种不同的方式来绘制立方体? 最佳答案 如果您以唯一可能的有趣方式解释它,那么这是一个比 3^6 更难的问题:有多少种不同的(即对称的)方法来为立方体着色。这是一篇论文:
我正在尝试解决优化问题,但首先我必须找到 n 个元素的所有可能组合的数量,但要考虑一些冲突。一个可能的例子是: 元素:{1,2,3,4}冲突:{1,2},{3,4} 术语“冲突”是指属于同一冲突集合的
Cleave 是一个非常有用的组合器,可以最大限度地减少代码重复。假设我要分类 Abundant, Perfect, Deficient numbers : USING: arrays assocs
有没有办法让 @Published 变量只在新值与旧值不同时才发布其值? 现在如果我们有 @Published var test: Bool = false 我们做到了 test = false te
有一个数组 [1, 2, ..., m] ,并且有一个整数 n . 如 m=2和 n=3 ,我想获得 [1, 1, 1] [1, 1, 2] [1, 2, 1] [1, 2, 2] [2, 1, 1]
我在我的应用程序中使用了一个用于日志记录页面的表单,并且在页脚上有一个绑定(bind)来显示任何错误,如下所示: 内容 View .Swift : Form { Section(footer: Tex
HTML first second third SCSS $statistics: ("first", "second", "third"); :root { --first: r
我有一个 HTTP 请求发布者,当返回 401 错误时,我想停止执行并显示我的登录屏幕。 这是我的代码的一部分: cancellable = fetcher.hello(helloRequest: H
我是一名优秀的程序员,十分优秀!