小编Nat*_*Fox的帖子

预期的约束寿命参数,找到具体的寿命

我无法弄清楚这段代码的生命周期参数.我尝试的所有内容通常会导致编译器错误:"预期绑定生命周期参数'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)

rust

12
推荐指数
1
解决办法
4775
查看次数

如何在从成员函数调用的闭包中修改self?

我正在尝试计算合法的国际象棋动作,并且在满足借阅检查器方面遇到了问题.我有一个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)

closures mutability rust borrow-checker

9
推荐指数
1
解决办法
2178
查看次数

预期特征 core::ops::FnMut,找到类型参数

我不明白为什么下面的代码不能编译。似乎 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)

rust

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

Java字节码如何处理多个平台?

例如,假设您有一个只打开窗口的java程序.这显然会导致不同操作系统的不同汇编代码(在Windows上最终必须调用CreateWindowEx).那么Java字节码(或任何其他类似语言)如何表示像这样的特定平台呢?

java bytecode

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

标签 统计

rust ×3

borrow-checker ×1

bytecode ×1

closures ×1

java ×1

mutability ×1