小编kez*_*zos的帖子

如果/ else阻塞,如何在迭代器中使用String而不是&str

有人会友好地向我解释为什么String在这个脚本中使用a 不起作用,但&str确实如此.另外,我如何修改它以便它可以用于String?[版本1.2]

use std::collections::{HashMap};

fn main() { 

    let mut hash = HashMap::<&str, &str>::new();
    hash.insert("this", "value");
    let l: &str = "this is a borrowed string reference";
    // If the above line was defined as:
    //let l: String = "this is a string".to_string();

    let mut all = l.split(" ");   
    let name: &str = all.next().unwrap();

    if hash.contains_key(name) == true {
        hash.remove(name);
    } else {
        hash.insert(name, "stuff");
    }
}
Run Code Online (Sandbox Code Playgroud)

rust

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

将argv转换为新数组

我无法弄清楚为什么这段代码不起作用.我已经看到了这一点,但我仍然没有得到它.我得到一个分段错误(11)并且看不到argv从中打印的数组testArray.

我的代码到目前为止

#include <iostream>

int main(int argc, char**argv) {

    char testArray[argc];

    for (int i=0; argc; i++){
        std::cout << argv[i] << std::endl;
        testArray[i] = *argv[i];
    }

    for (int i=0; argc; i++){
        std::cout << testArray[i] << std::endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays

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

从进程stdout读取而不将其全部放入内存中

我试图捕获外部工具的输出,该工具在一个单独的进程中运行.我想以非阻塞的方式这样做,因为输出大于内存.我看到你如何从Rust中的Process流输出?但我不知道该怎么办.我从这里复制了一个例子,但这似乎在继续之前将输出捕获到变量中.到目前为止,我有:

let path = "/Documents/Ruststuff/DB30_igh_badPE.bam";
let output = Command::new("samtools")
    .arg("view")
    .arg("-H")
    .arg(path)
    .stdin(Stdio::piped())
    .stdout(Stdio::piped())
    .spawn()
    .unwrap_or_else(|e| panic!("failed {}", e));

let mut s = String::new();
match output.stdout.unwrap().read_to_string(&mut s) {
    Err(why) => panic!("{}", Error::description(&why)),
    Ok(_) => print!("{}", s),
}
Run Code Online (Sandbox Code Playgroud)

是否可以stdout从子进程循环而不是使用匹配?就像是:

for line in &output.stdout {}
Run Code Online (Sandbox Code Playgroud)

process rust

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

取消引用运算符和endl没有输出

我试图让我的头围指针,并且无法理解为什么使用dereference运算符来打印一个值在"\n";添加到行时工作正常,但由于某种原因我在使用时没有得到任何输出endl;.终端显示没有输出endl;.这与输出缓冲区刷新有关吗?

#include <iostream>
using namespace std;

int main()
{
    int arrayA[] = {0, 1, 2, 3, 4, 5};
    int * ptr_P;

    ptr_P = arrayA;
    for (int i; i < 6; i++)
    {
        cout << *ptr_P << "\n"; // Works fine, but endl; does not
        ptr_P++;
    }
   return(0);
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers

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

在功能样式中将点附加到嵌套列表

我试图找到一种使用函数样式将元组附加到嵌套列表的好方法.我想要替换的代码是:

a = [[], []]

point = [(10, 12), (11, 13), (14, 15)]

for item in point:
    a[0].append(item[0])
    a[1].append(item[1])
>>> [[10, 11, 14], [12, 13, 15]]
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经想出了这个,但似乎我已经过度复杂了,并且想知道是否有更好的方法:

from functools import partial

map(partial(lambda a, b, c: (a.append(c[0]), b.append(c[1])), a[0], a[1]), point)
print a
>>> [[10, 11, 14], [12, 13, 15]]
Run Code Online (Sandbox Code Playgroud)

python list

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

标签 统计

c++ ×2

rust ×2

arrays ×1

list ×1

pointers ×1

process ×1

python ×1