小编iop*_*opq的帖子

函数返回一个闭包不在我的过滤器内工作

我不能在不使用闭包的情况下编译它.我试图让函数apply首先返回正确的闭包.

#![feature(conservative_impl_trait)]
#![allow(dead_code)]

fn accumulate<'a>(tuples: &[(&'a str, &Fn(i32) -> bool)], i: i32) {

    // this works
    let _ = tuples.iter().filter(|t| apply(second, i)(t));

    // this doesn't
    //let f = apply(second, i);
    //let _ = tuples.iter().filter(f);

    //this works as well

    let f  = |t: &&(_,_)| apply(second, i)(t);
    let _ = tuples.iter().filter(f);
}

fn apply<A, B, C, F, G>(mut f: F, a: A) -> impl FnMut(B) -> C
         where F: FnMut(B) -> G,
               G: FnMut(A) -> C,
               A: Clone
{
    move …
Run Code Online (Sandbox Code Playgroud)

closures rust trait-objects

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

jade模板中的多行无缓冲代码

我正在尝试编写一个呈现双索引数组的模板.所以我开始写这个:

- var grid = [[1, 0, 1], [0, 1, 0]];
each row in grid
    each cell in row
        if cell
            span x
        else
            span o
Run Code Online (Sandbox Code Playgroud)

但这不是我想写我的数组我想写这样的:

- var grid = [[1, 0, 1],
              [0, 1, 0]];
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为jade已经不在内联javascript中

- var grid = [[1, 0, 1],
             - [0, 1, 0]];
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为玉认为这两个不正确的行而不是一行

我怎样才能使它工作?

node.js pug

8
推荐指数
2
解决办法
4463
查看次数

水线ORM等效于重复密钥更新时的插入

我有一个表user_address,它有一些字段,如

attributes: {
  user_id: 'integer',
  address: 'string' //etc.
}
Run Code Online (Sandbox Code Playgroud)

目前我这样做是为了插入新记录,但如果该记录存​​在,请更新它:

UserAddress
  .query(
    'INSERT INTO user_address (user_id, address) VALUES (?, ?) ' +
      'ON DUPLICATE KEY UPDATE address=VALUES(address);',
    params,
    function(err) {
       //error handling logic if err exists
    }
Run Code Online (Sandbox Code Playgroud)

有没有办法使用Waterline ORM而不是直接的SQL查询来实现同样的目的?我不想做两个查询,因为它效率低,难以维护.

sql orm node.js sails.js waterline

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

更高级别的生命周期和泛型不能很好地发挥

在这里的代码中

trait Foo {
    type Output;
    fn foo(self) -> Self::Output;
}

impl<'a> Foo for &'a () {
    type Output = &'a ();
    fn foo(self) -> Self::Output {
        self
    }
}

fn func<F: Foo>(f: F) -> F::Output {
    f.foo()
}

fn func2<'a>(f: &'a ()) -> &'a () {
    func::<&'a ()>(f)
}

fn has_hrl<F: Fn(&()) -> &()>(f: F) {}

fn main() {
    //has_hrl(func); // FAILS
    has_hrl(func2);
    has_hrl(|x| func(x));
}
Run Code Online (Sandbox Code Playgroud)

我们想做has_hrl(func),但Rust只接受关闭has_hrl(|x| func(x)).这是为什么?因为它适用于类似的具体类型func2,但不适用于泛型类型.

closures traits lifetime rust

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

标签 统计

closures ×2

node.js ×2

rust ×2

lifetime ×1

orm ×1

pug ×1

sails.js ×1

sql ×1

trait-objects ×1

traits ×1

waterline ×1