小编Nat*_*ara的帖子

Git显示整个文件的变化

有没有办法让git show命令在查看提交时显示文件的全部内容?例如:如果它当前显示类似的东西

foo.cpp

+++ int main() {
+++    std::cout << "HELLO" << std::endl;
+++ }
Run Code Online (Sandbox Code Playgroud)

我希望输出说:

foo.cpp

#include <stdio> //assuming this was from an earlier commit

+++ int main() {
+++    std::cout << "HELLO" << std::endl;
+++ }
Run Code Online (Sandbox Code Playgroud)

有一个简单的方法吗?

git diff git-diff git-show

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

为什么这个C++代码与一些编译器编译而不是其他编译器?

我在做作业时注意到我的笔记本电脑上的编译器比我们预期用于提交的机器上的编译器更宽松.我的笔记本电脑上的C++编译器是AppleClang 7.0.2.7000181,并且提交框上的编译器是g++ 4.9.2.回想起来不应该编译的代码是:

#include <iostream>

std::tuple<int, int> foo() {
    return std::make_tuple(1, 1);
}

int main() {
    auto pair = foo();
    int x = std::get<0>(pair);
    int y = std::get<1>(pair);
    std::cout << x << "," << y << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我也有一个CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(foo)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Werror -Werror=sign-compare")

set(SOURCE_FILES main.cpp)
add_executable(foo ${SOURCE_FILES})
Run Code Online (Sandbox Code Playgroud)

在我的笔记本电脑上,Clang愉快地编译了这段代码并打印出来1,1.没有错误,没有警告,没有.在提交框中,我没那么幸运.

/home/nate/foo/main.cpp: In function 'std::tuple<int, int> foo()':
/home/nate/foo/main.cpp:3:26: error: return type 'class std::tuple<int, int>' is incomplete
 std::tuple<int, int> foo() { …
Run Code Online (Sandbox Code Playgroud)

c++

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

多个异步上下文管理器

是否可以在python中组合异步上下文管理器?类似于asyncio.gather,但能够与上下文管理器一起使用的东西.像这样的东西:

async def foo():
    async with asyncio.gather_cm(start_vm(), start_vm()) as vm1, vm2:
        await vm1.do_something()
        await vm2.do_something()
Run Code Online (Sandbox Code Playgroud)

这目前可能吗?

python python-asyncio

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

Rust TT muncher 具有无限递归

我正在尝试使用 TT muncher 创建嵌套哈希映射结构。基本类型定义是

type Object = HashMap<String, Node>;

enum Node {
    Terminal(String),
    Nested(Object),
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以手动创建这些对象:

let mut x: Object = HashMap::new();

x.insert("foo".into(), Node::Terminal("bar".into()));
x.insert("bing".into(), {
    let mut bing = HashMap::new();
    bing.insert("bar".into(), Node::Terminal("baz".into()));
    Node::Nested(bing)
});
Run Code Online (Sandbox Code Playgroud)

这确实生成了预期的结构

{
    "bing": Nested(
        {
            "bar": Terminal(
                "baz"
            )
        }
    ),
    "foo": Terminal(
        "bar"
    )
}
Run Code Online (Sandbox Code Playgroud)

但我有一些这种格式的大文字,而且我更喜欢使用不太难看的语法,所以我尝试制作一个宏。这是我认为应该起作用的最小示例:

use std::collections::HashMap;

type Object = HashMap<String, Node>;


#[derive(Debug)]
enum Node {
    Terminal(String),
    Nested(Object),
}


macro_rules! obj {
    {
        $($tt:tt)*
    } => {
        {
            let map = ::std::collections::HashMap::new();

            obj!(@parse; …
Run Code Online (Sandbox Code Playgroud)

rust

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

在 Ansible 角色任务文件中使用 YAML 锚点

我正在尝试创建一个包含多个重复部分的 ansible 角色任务文件,并且我想利用 YAML 的锚点功能,该功能允许跨文件共享数据。在我的实际文件中,我有 3 或 4 个属性需要在我的文件中的十几个任务中完全相同,因此锚点似乎是完美的解决方案。这是我的设置:

主机配置文件

localhost connection=local
Run Code Online (Sandbox Code Playgroud)

测试文件

---
- name: test
  hosts: localhost
  roles:
    - test
Run Code Online (Sandbox Code Playgroud)

角色/测试/任务/main.yml

---
foo: &foo
  msg: 'this is a test'

- name: Test message
  debug:
    <<: *foo
Run Code Online (Sandbox Code Playgroud)

我希望foo字典的属性应该扩展到debug字典中,从而产生类似的结构

{
  "name": "Test message",
  "debug": {
    "msg": "this is a test"
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试运行剧本时,却收到了此错误消息:

? ansible-playbook -i hosts.ini test.yml
ERROR! Syntax Error while loading YAML.
  did not find expected key

The error appears to have been in …
Run Code Online (Sandbox Code Playgroud)

ansible

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

Bash脚本 - 迭代find的输出

我有一个bash脚本,我需要遍历find命令的输出的每一行,但看起来我正在从find命令迭代每个Word(空格分隔).到目前为止我的脚本看起来像这样:

folders=`find -maxdepth 1 -type d`

for $i in $folders
do
    echo $i
done
Run Code Online (Sandbox Code Playgroud)

我希望这会给出如下输出:

./dir1 and foo
./dir2 and bar
./dir3 and baz
Run Code Online (Sandbox Code Playgroud)

但我得到这样的输出:

./dir1
and
foo
./dir2
and
bar
./dir3
and
baz
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

linux bash shell scripting

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

移动并返回一个可变指针

我正在通过这个 Rust教程,我正在尝试解决这个问题:

实现一个函数,incrementMut它将整数向量作为输入,并通过将每个值递增1来修改原始列表的值.

这似乎是一个相当简单的问题,是吗?

我一直试图找到一段时间的编译解决方案,但我开始失去希望.这是我到目前为止:

fn main() {
    let mut p = vec![1i, 2i, 3i];
    increment_mut(p);

    for &x in p.iter() {
        print!("{} ", x);
    }
    println!("");
}

fn increment_mut(mut x: Vec<int>) {
    for &mut i in x.iter() {
        i += 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是编译器在我尝试编译时所说的内容:

Compiling tut2 v0.0.1 (file:///home/nate/git/rust/tut2)
/home/nate/git/rust/tut2/src/main.rs:5:12: 5:13 error: use of moved value: `p`
/home/nate/git/rust/tut2/src/main.rs:5  for &x in p.iter() {
                                                  ^
/home/nate/git/rust/tut2/src/main.rs:3:16: 3:17 note: `p` moved here because it has type `collections::vec::Vec<int>`, which is non-copyable …
Run Code Online (Sandbox Code Playgroud)

memory ownership rust

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

标签 统计

rust ×2

ansible ×1

bash ×1

c++ ×1

diff ×1

git ×1

git-diff ×1

git-show ×1

linux ×1

memory ×1

ownership ×1

python ×1

python-asyncio ×1

scripting ×1

shell ×1