小编hxw*_*ker的帖子

如何通过副作用实现变量增量?

出于学习目的,我尝试了这个解决方案,但它不起作用:

use std::ops::Add;

fn inc<T: Add>(x:&mut T) {
    *x += 1; 
}

fn main() {
    let mut x:i32 = 10;
    let mut y:u8 = 1;
    inc(&mut x);
    inc(&mut y);
    println!("{} {}", x, y);
}
Run Code Online (Sandbox Code Playgroud)

错误信息:

<anon>:4:5: 4:7 error: binary assignment operation `+=` cannot be applied to types `T` and `_` [E0368]
<anon>:4     *x += 1; 
             ^~
<anon>:4:5: 4:7 help: see the detailed explanation for E0368
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?

side-effects rust

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

如何防止在ZSH中执行命令?

我为命令行编写了钩子:

# Transforms command 'ls?' to 'man ls'

function question_to_man() {
    if [[ $2 =~ '^\w+\?$' ]]; then 
        man ${2[0,-2]}
    fi
}

autoload -Uz add-zsh-hook

add-zsh-hook preexec question_to_man
Run Code Online (Sandbox Code Playgroud)

但当我这样做时:

> ls?
Run Code Online (Sandbox Code Playgroud)

退出后man我得到:

> zsh: no matches found: ls?
Run Code Online (Sandbox Code Playgroud)

如何摆脱有关错误命令的消息?

linux zsh

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

标签 统计

linux ×1

rust ×1

side-effects ×1

zsh ×1