我无法弄清楚这段代码的生命周期参数.我尝试的所有内容通常会导致编译器错误:"预期绑定生命周期参数'a
,找到具体生命周期"或类似"考虑使用显示的显式生命周期参数"(并且显示的示例没有帮助)或"与特征不兼容的方法" ".
Request
,Response
和,Action
是简化版本,以保持此示例最小.
struct Request {
data: String,
}
struct Response<'a> {
data: &'a str,
}
pub enum Action<'a> {
Next(Response<'a>),
Done,
}
pub trait Handler: Send + Sync {
fn handle<'a>(&self, req: Request, res: Response<'a>) -> Action<'a>;
}
impl<'a, T> Handler for T
where
T: Send + Sync + Fn(Request, Response<'a>) -> Action<'a>,
{
fn handle(&self, req: Request, res: Response<'a>) -> Action<'a> {
(*self)(req, res)
}
}
fn main() {
println!("running");
} …
Run Code Online (Sandbox Code Playgroud) 我正在尝试计算合法的国际象棋动作,并且在满足借阅检查器方面遇到了问题.我有一个Chess
实现这些方法的结构(替换为非重要代码...
):
// internal iterator over (possibly not legal) moves
fn get_moves<F>(&self, func: F)
where
F: Fn(/* ... */),
{
func(/* ... */); // move 1
func(/* ... */); // move 2
func(/* ... */); // etc...
}
fn is_legal_move(&mut self) -> bool {
// notice this takes a mutable self. For performance
// reasons, the move is made, legality is checked, then I
// undo the move, so it must be mutable to be able to …
Run Code Online (Sandbox Code Playgroud) 我不明白为什么下面的代码不能编译。似乎 rust 只是没有“扩展”类型参数,因为它看起来与我匹配。
代码(防锈围栏:http : //is.gd/gC82I4)
use std::sync::{Arc, Mutex};
struct Data{
func: Option<Box<FnMut(String) + Send>>
}
fn newData<F>(func: Option<Box<F>>) -> Data
where F: FnMut(String) + Send{
Data{
func: func
}
}
fn main(){
let _ = newData(Some(Box::new(|msg|{})));
}
Run Code Online (Sandbox Code Playgroud)
错误
<anon>:10:15: 10:19 error: mismatched types:
expected `core::option::Option<Box<core::ops::FnMut(collections::string::String) + Send>>`,
found `core::option::Option<Box<F>>`
(expected trait core::ops::FnMut,
found type parameter) [E0308]
<anon>:10 func: func
^~~~
error: aborting due to previous error
playpen: application terminated with error code 101
Run Code Online (Sandbox Code Playgroud) 例如,假设您有一个只打开窗口的java程序.这显然会导致不同操作系统的不同汇编代码(在Windows上最终必须调用CreateWindowEx).那么Java字节码(或任何其他类似语言)如何表示像这样的特定平台呢?