小编Ray*_*bel的帖子

如何在shell中运行C函数?

在学习C的同时,我认识到你可以在linux shell中看到它的函数手册(我一直在使用BASH).例如:

man strlen
man crypt
man printf
Run Code Online (Sandbox Code Playgroud)

我想我也许可以在shell脚本中使用这些函数.

这是真的?如何在shell脚本中使用这些函数?

c linux bash shell manpage

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

从分隔文件中解析变量

我有一些由管道|符号分隔的文件内容。名为,important.txt.

1|130|80|120|110|E
2|290|420|90|70|B
3|100|220|30|80|C
Run Code Online (Sandbox Code Playgroud)

然后,我使用 RustBufReader::split来读取其内容。

use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::io::Prelude::*;
use std::path::Path;

fn main() {
    let path = Path::new("important.txt");
    let display = path.display();

    //Open read-only
    let file = match File::open(&path) {
        Err(why) => panic!("can't open {}: {}", display,
                           Error::description(why)),
        Ok(file) => file,
    }

    //Read each line
    let reader = BufReader::new(&file);
    for vars in reader.split(b'|') {
        println!("{:?}\n", vars.unwrap());
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,vars.unwrap()会返回字节而不是字符串。

[49]
[49, 51, 48]
[56, 48]
[49, 50, …
Run Code Online (Sandbox Code Playgroud)

rust

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

如何返回具有单位数据类型的结构?

我正在尝试实现自定义集.这可以编译没有问题:

struct CustomSet {}

impl CustomSet {
    pub fn new() -> CustomSet {
        CustomSet {}
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试将单元类型(空元组)添加到CustomSet类型中时,它将无法编译.

struct CustomSet<()> {}

impl CustomSet<()> {
    pub fn new() -> CustomSet<()> {
        CustomSet {}
    }
}
Run Code Online (Sandbox Code Playgroud)

以下错误

error: expected one of `>`, identifier, or lifetime, found `(`
 --> src/lib.rs:1:18
  |
1 | struct CustomSet<()> {}
  |                  ^ expected one of `>`, identifier, or lifetime here
Run Code Online (Sandbox Code Playgroud)

如何返回具有单位数据类型的结构?我做错了什么?

generics syntax struct tuples rust

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

标签 统计

rust ×2

bash ×1

c ×1

generics ×1

linux ×1

manpage ×1

shell ×1

struct ×1

syntax ×1

tuples ×1