小编Mic*_*ard的帖子

为什么带有保护子句的匹配模式不详尽?

考虑以下代码示例(playground)。

#[derive(PartialEq, Clone, Debug)]
enum State {
    Initial,
    One,
    Two,
}

enum Event {
    ButtonOne,
    ButtonTwo,
}

struct StateMachine {
    state: State,
}

impl StateMachine {
    fn new() -> StateMachine {
        StateMachine {
            state: State::Initial,
        }
    }

    fn advance_for_event(&mut self, event: Event) {
        // grab a local copy of the current state
        let start_state = self.state.clone();

        // determine the next state required
        let end_state = match (start_state, event) {
            // starting with initial
            (State::Initial, Event::ButtonOne) => State::One,
            (State::Initial, …
Run Code Online (Sandbox Code Playgroud)

pattern-matching rust

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

在不同的机器上使用不同的git子模块URL

有没有办法在一台机器上使用一个子模块URL,但在另一台机器上使用不同的子模块URL?

假设我有一台机器对中央服务器上的git存储库进行了身份验证SSH访问,而另一台机器无法访问该服务器.在经过身份验证的计算机上,我习惯git-daemon将存储库共享给未经身份验证的计算机.

这里的问题是我克隆的存储库有几个子模块依赖项,所有这些都是服务器上需要经过身份验证的SSH访问的存储库.在这种情况下,我还将所有子模块存储在我的经过身份验证的计算机上,因此我只需更新.gitmodules文件:

[submodule "@sub-a"]
    url = ssh://<authenticated-url>/<repo>
    branch = master
Run Code Online (Sandbox Code Playgroud)

至...

[submodule "@sub-a"]
    url = git://<ip-of-authenticated-machine>/<repo>
    branch = master
Run Code Online (Sandbox Code Playgroud)

然后运行git submodule sync并将git submodule update --init -r直接从我的经过身份验证的计算机而不是从服务器获取子模块.

我的问题是,有没有可行的方法来做到这一点,以便我可以在机器之间自由移动,并期望正确的git-submodule行为

git

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

如何测试容器中所有项目的值?

假设我有一个容器,如字典或列表.如果容器的所有值都等于给定值(例如None),那么测试Python的方法是什么?

我天真的实现是只使用一个布尔标志,就像我在C中所做的那样,所以代码可能看起来像.

a_dict = {
    "k1" : None,
    "k2" : None,
    "k3" : None
}

carry_on = True
for value in a_dict.values():
    if value is not None:
        carry_on = False
        break

if carry_on:
    # action when all of the items are the same value
    pass

else:
    # action when at least one of the items is not the same as others
    pass
Run Code Online (Sandbox Code Playgroud)

虽然这种方法运行得很好,但考虑到Python如何处理其他常见模式,它感觉不对.这样做的正确方法是什么?我想也许内置all()函数会做我想要的,但它只测试布尔上下文中的值,我想比较任意值.

python validation containers python-3.x

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