小编Dan*_*kov的帖子

如何将文字值转换为特征对象?

我试图使这些代码更简洁,并摆脱冗余变量.有可能这样做吗?

trait Foo {}
impl Foo for i32 {}
impl Foo for String {}

fn main() {
    let xi32: i32 = 10;
    let y = String::from("ooo");
    let mut foo_list: Vec<&Foo> = vec![];
    foo_list.push(&xi32 as &Foo);
    foo_list.push(&y as &Foo);
}
Run Code Online (Sandbox Code Playgroud)

以下变体不起作用:

foo_list.push(10 as Foo);
Run Code Online (Sandbox Code Playgroud)
error[E0620]: cast to unsized type: `{integer}` as `Foo`
  --> src/main.rs:11:19
   |
11 |     foo_list.push(10 as Foo);
   |                   ^^^^^^^^^
   |
help: consider using a box or reference as appropriate
  --> src/main.rs:11:19
   |
11 |     foo_list.push(10 as Foo);
   | …
Run Code Online (Sandbox Code Playgroud)

rust

5
推荐指数
2
解决办法
1133
查看次数

包装类型时“在当前范围内找不到类型 T 的方法”

我正在尝试围绕两种不同类型制作适配器来完成相同的工作,但我无法重写这两种类型。

X有一个消耗的方法self,因此运行时多态包装器不适用。唯一的选择是静态通用方法。

struct X {}

impl X {
    fn f(self, n: i32) {
        println!("n = {}", n);
    }
    fn new() -> X {
        X {}
    }
}

struct AdapterX {
    x: X
}

impl AdapterX {
    fn uf(self, n: i32) {
        self.x.f(n)
    }
    fn new(x: X) -> AdapterX {
        AdapterX { x: x }
    }
}

fn useAdapted<T>(a: T) {
    a.uf(10)
}

fn main() {
    let x = X::new();
    useAdapted::<AdapterX>(AdapterX::new(x));
}
Run Code Online (Sandbox Code Playgroud)

编译器失败并显示:

struct X {}

impl X …
Run Code Online (Sandbox Code Playgroud)

adapter rust

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

如何使用 DOM 测试 angularjs 组件

我正在尝试熟悉测试 AngularJS 应用程序。虽然测试组件逻辑或多或少是清楚的,但我在 html 模板和模型绑定方面遇到了麻烦,因为我想将 html 绑定与控制器的逻辑一起测试。

测试在真实浏览器中由 karma 运行,因此测试环境支持 DOM。

看起来不可能,不是吗?

describe('sign-up', function () {
        angular.mock.module('myApp');
        angular.mock.inject(function($componentController, $rootScope, $document) {
            let scope = $rootScope.$new();
            let signUp = $componentController('signUp', {$scope: scope});
            console.log(`signup = ${signUp}`);
            for (let [k,v] of signUp) {
                // there is no field signUp.firstName
                // but inside the controller code referencing  
                // this.firstName is working
                console.log(`signup.${k} = ${v}`);
            }
            // jquery cannot find #firstName node
            // $('#firstName').val('dan') gets the same outcome
            $document.find('#firstName').val('dan');
            expect($document.find('#firstName').val()).toBe('dan');
            // without DOM form submission …
Run Code Online (Sandbox Code Playgroud)

javascript testing angularjs angular-mock

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

如何将 git 修订版注入 Nix 推导中?

有一些库,例如 gitrev,它允许将 git 修订版编译为可执行文件,但在使用nix-build http://../mastergit 的情况下,源树中会丢失元数据。

在这种情况下如何引用派生修订版以至少将其包含在 shellHook 内?

git nix

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

如何逃避教育署的时期

我正在研究ed文本编辑器。

要退出输入模式,用户应在单个句点(.)中输入一行。

假设我要输入句点作为文本。

我想到了一种解决方法:首先,我插入..。然后,我替换...

但是我的方法有点笨拙。有一个更好的方法吗?

bash ed

4
推荐指数
2
解决办法
418
查看次数

如何在PropertySource中使用相对于用户主目录的路径

Spring表达式在PropertySource注释中不起作用.

@PropertySource({ "classpath:application.properties",
        "#{systemProperties['user.home']}/.app.properties" })
@Configuration
public class Config {
@Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer result = new PropertySourcesPlaceholderConfigurer();
        result.setOrder(0);
        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

java spring annotations applicationcontext

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

如何将文字字符串与const字符串连接?

我试图实例化一个参数解析器(clap).有代码如:

const DEFAULT_VALUE: &'static str = "12312312";
// ...
.help("this parameter is for (default:" + DEFAULT_VALUE + ")")
// ...
Run Code Online (Sandbox Code Playgroud)

我查看了类似的现有问题并发现了concat!宏和lazy_static.

第一个选项不合适,lazy_static没有示例.如果可能的话,它将会过于复杂,因为lazy_static需要在一个单独的地方定义一个块.

我正在寻找一些简洁的语法糖与一个宏,没有很多类型的开销.

如果定义一个局部变量,它可能会变高,因为拍拍的DSL可能会很长.它不方便,因为它从代码中的逻辑位置撕掉了字符串.

另一种为整个帮助字符串定义静态变量的方法,但它具有与上述方法相同的缺点以及命名空间污染.


建议的格式解决方案!也不适合.它需要定义一个局部变量.


extern crate clap;

use clap::{Arg, App};

const DEFAULT: &'static str = "1";

fn main() {
    let params = App::new("app")
        .arg(Arg::with_name("p")
             // here 100 lines of the uninterruptable expression
             .help(&format!("parameter p (DEFAULT: {})", DEFAULT)))
             // here also 100 lines of the uninterruptable expression
        .get_matches();
    println!("param p = {}", params.value_of("p").unwrap())
}
Run Code Online (Sandbox Code Playgroud)

Cargo.toml

[package]

name = …
Run Code Online (Sandbox Code Playgroud)

rust

4
推荐指数
2
解决办法
1816
查看次数

如何将命令行参数传递给docker镜像

我在docker镜像中运行测试,我需要一直传递自定义参数.

当我在图像名称docker认为参数是图像名称后放置参数.

docker run  -t -i image-name -s test.py
docker run  -t -i image-name -- -s test.py
Run Code Online (Sandbox Code Playgroud)

错误:

Failed no image test_arena2.py
Run Code Online (Sandbox Code Playgroud)

Docker版本1.11.2,构建b9f10c9

docker

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

如何在 GHCi 中列出具有应用类型参数的类型的实例

我注意到我不知道如何制作关于复合类型的 GHCi 打印信息。让我们考虑例子

data X a = X (a Int)
type XList = X []

instance Show XList where show (X l) = "X (" ++ show l ++ ")"
Run Code Online (Sandbox Code Playgroud)

我想看看“X []”是如何实现 Show 的。

尝试 1

? :i (X [])

<interactive>:1:2: error: parse error on input ‘X’
Run Code Online (Sandbox Code Playgroud)

尝试 2 - 打印列表的实例但不打印 (X [])

? :i X []
Run Code Online (Sandbox Code Playgroud)

尝试 3 - 与实例无关

? :i XList
type XList = X []   -- Defined at <interactive>:20:1
Run Code Online (Sandbox Code Playgroud)

尽管如此,显示实例在适用时正在工作

? show (X [1,2,3])
"X ([1,2,3])" …
Run Code Online (Sandbox Code Playgroud)

haskell ghci

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

如何在scala中构造无限的不可变树

我正在尝试将Java的代码片段转换为Scala。它是无限的树状结构。

class Node {
    public Node(Map<String, Node> routes) {}    
}

class Factory {
    public Map<String, Node> buildRoutes() {
        Map<String, Node> routes = new HashMap<>();
        asList("one", "two").forEach(key -> routes.put(key, new Node(routes));
        return routes;
    }
Run Code Online (Sandbox Code Playgroud)

运行正常。将上面的片段翻译成Scala之后,我发现Map出现了问题。看起来在Scala中,可变地图和不可变地图不兼容。

因此,我必须在“节点”字段中使用可变映射类型。对于我来说,这看起来很奇怪,因为我的地图不可变。它在Node中没有更改,Node必须稍微了解其应有的依赖性。

import scala.collection.mutable

case class Node(routes: mutable.Map[String, Node])

object Factory {
   def buildRoutes(): mutable.Map[String, Node] = {
      val routes = new mutable.HashMap[String, Node]
      List("one", "two").foreach(key => routes.put(key, Node(routes)))
      routes
   }
}

Run Code Online (Sandbox Code Playgroud)

我很高兴看到Node.routes是不可变映射的替代解决方案。

types scala

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