- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 rust 代码无法编译。
struct Person {
name : String,
age : u8,
}
fn main() {
let p = Person{ name: "Nobody".to_string(), age : 24};
let age = |p : &Person| p.age;
let name = |p : &Person | &p.name;
println! ("name={}, age={}" , name(&p), age(&p));
}
编译器给出了以下错误信息。
Compiling playground v0.0.1 (/playground)
error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
--> src/main.rs:11:31
|
11 | let name = |p : &Person | &p.name;
| ^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 11:16...
--> src/main.rs:11:16
|
11 | let name = |p : &Person | &p.name;
| ^^^^^^^^^^^^^^^^^^^^^^
note: ...so that reference does not outlive borrowed content
--> src/main.rs:11:31
|
11 | let name = |p : &Person | &p.name;
| ^^^^^^^
note: but, the lifetime must be valid for the expression at 2:29...
--> src/main.rs:13:5
|
13 | println! ("name={}, age={}" , name(&p), age(&p));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so type `(&&std::string::String, &u8)` of expression is valid during the expression
--> src/main.rs:13:5
|
13 | println! ("name={}, age={}" , name(&p), age(&p));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error: aborting due to previous error
我尝试为 name
闭包添加生命周期。
let name<'a> = |p : &'a Person | -> &'a String { &'a p.name };
还是编译出错
Compiling playground v0.0.1 (/playground)
error: expected one of `:`, `;`, `=`, `@`, or `|`, found `<`
--> src/main.rs:12:13
|
12 | let name<'a> = |p : &'a Person | -> &'a String { &'a p.name };
| ^ expected one of `:`, `;`, `=`, `@`, or `|`
error: aborting due to previous error
只是想知道如何编写正确的代码。
最佳答案
另一种解决方案是为您的闭包提供显式类型。遗憾的是,您不能使用它的实际类型,但可以将其转换为函数指针。
请记住,问题在于编译器无法正确推断输出的生命周期与输入的生命周期相关(它可能是 this bug 的一个实例,但我根本不是当然)。我们可以通过使生命周期显式化来解决这个问题。
struct Person {
name: String,
age: u8,
}
fn main() {
let p = Person {
name: "Nobody".to_string(),
age: 24,
};
let age = |p: &Person| p.age;
// Our only changes are right here.
let name: for<'a> fn(&'a Person) -> &'a String = |p: &Person| &p.name;
println!("name={}, age={}", name(&p), age(&p));
}
事实上,它可能比这稍微不那么明确。编译器可以很好地确定输入和输出的类型。这只是它遇到麻烦的生命周期。所以用 let name: for<'a> fn(&'a _) -> &'a _ = |p: &Person| &p.name;
替换该行也适用 (playground) .
关于rust - 如何在 Rust Closure 中返回引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60897823/
我刚刚开始使用 Google Closure 做一些工作,我想将选择字段的创建移动到它自己的模板中,并使用类似以下内容调用该模板: {call templates.utils.select} {p
我有一些代码,简化后看起来像: fn foo() -> Vec { unsafe { unsafe_iterator().map(|n| wrap_element(n)).co
我正在从脚本内部调用Closure Compiler(closurecompiler.jar)。该脚本还生成Closure Compiler需要编译的一些javascript。有没有办法将此JavaS
以下示例代码生成有关高级优化的编译器警告:“JSC_UNSAFE_NAMESPACE:为命名空间 NS 创建的别名不完整”。如果我删除@enum 注释,它不会发出警告。 var NS = {}; /*
看代码: let add_one = |&: x| { 1 + x }; 我知道x是闭包参数,但是闭包中的&:是什么意思? 最佳答案 这是 Rust 的一个文档不足的部分(并且过时,请参阅评论)。我知
PHP manual for anonymous functions (即闭包)指出: Anonymous functions are currently implemented using the
我从脚本内部调用 Closure Compiler (closurecompiler.jar)。该脚本还生成了一些 Closure Compiler 需要编译的 javascript。有没有办法将这个
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 7年前关闭。 Improve t
当鼠标在文档正文中移动时,我试图调用一个函数。但是,下面的事件处理程序不起作用。 goog.events.listen(document, 'onmousemove', function(e)
我试过了 java -jar compiler.jar --js jj.js --js_output_file jj.js 输出文件大小为 0。 如果我不想从 .min.js 重命名为 .js,我该怎
Google Closure UI库如何与Google DART一起使用? 最佳答案 Dart没有使用JavaScript库的功能。这是设计使然,因为Dart旨在同时针对Dart VM和转换为JS的D
是否可以使用 Google Closure 编译器在两个文件中定义一个类?例如,如果我自动生成一个类并希望为用户输入的代码保留另一个类: 在 MyClass.AutoGenerated.js 中 go
当我在 http://closure-compiler.appspot.com 处的闭包编译器中测试以下代码时: // ==ClosureCompiler== // @output_file_name
是否可以使用 Google Closure 编译器在两个文件中定义一个类?例如,如果我自动生成一个类并希望为用户输入的代码保留另一个类: 在 MyClass.AutoGenerated.js 中 go
当我运行闭包编译器时,会收到一堆这样的警告: [exec] jquery/3.2.1/dist/jquery.js:733: WARNING - Suspicious code. The resul
假设您正在一个具有多个外部库依赖项的 javascript 项目中工作,并且想要在 ADVANCED_OPTIMIZATIONS 模式下使用 Google Closure Compiler 编译您的源
我正在为 PIXI.js 库准备 externs。我收到以下警告: js/Test.js:188: WARNING - Property position never defined on PIXI.
我最近使用 Google 的 Closure 编译器创建了一个 JavaScript 库:https://github.com/bvaughn/task-runner 我打算让这个库供那些也需要完整闭
我正在尝试自学闭包模板。我做了一个简单的文件 simple.soy: {namespace examples.simple} /** * says hello to the world * @pa
我正在将一个项目从 jQuery 迁移到 Closure。我有一些我想编译的只迁移了一半的代码。未编译的源工作正常。我想知道使用 SIMPLE_OPTIMIZATIONS 编译它的编译命令。 原始基于
我是一名优秀的程序员,十分优秀!