由于Rust比较新,我看到了很多阅读和编写文件的方法.许多人为他们的博客提出了非常混乱的片段,我发现的99%的例子(即使在Stack Overflow上)来自不稳定的构建,不再有效.现在Rust是稳定的,什么是简单,可读,非恐慌的读取或写入文件片段?
这是我最接近阅读文本文件的东西,但它仍然没有编译,即使我很确定我已经包含了我应该拥有的一切.这是基于我在所有地方的Google+上发现的一个片段,我唯一改变的是旧BufferedReader的现在只是BufReader:
use std::fs::File;
use std::io::BufReader;
use std::path::Path;
fn main() {
let path = Path::new("./textfile");
let mut file = BufReader::new(File::open(&path));
for line in file.lines() {
println!("{}", line);
}
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨:
error: the trait bound `std::result::Result<std::fs::File, std::io::Error>: std::io::Read` is not satisfied [--explain E0277]
--> src/main.rs:7:20
|>
7 |> let mut file = BufReader::new(File::open(&path));
|> ^^^^^^^^^^^^^^
note: required by `std::io::BufReader::new`
error: no method named `lines` found for type `std::io::BufReader<std::result::Result<std::fs::File, std::io::Error>>` in the current scope
--> src/main.rs:8:22
|> …Run Code Online (Sandbox Code Playgroud) 这是我一直在考虑的事情.我已经做了一些研究而且找不到任何东西,但我也没有发现任何相反的东西.
考虑一下这个std::sort功能<algorithm>.它需要两个迭代器和一个函数指针作为参数.所以,如果我想按字母顺序对字符串向量进行排序,我会这样做:
bool ascending(std::string lhs, std::string rhs) { return lhs < rhs; }
std::sort(my_vector.begin(), my_vector.end(), ascending);
Run Code Online (Sandbox Code Playgroud)
问题是这种类型的排序函数区分大小写,因此在以大写"Z"开头的字符串后面会放置以小写"a"开头的字符串.我看到的唯一可见的解决方案是创建一个额外的功能bool ascending_case_insensitive().但是,如果我可以在sort中使用bool ascending()带有附加bool is_case_sensitive参数的函数,那就太好了.这可能吗?
我正在查看Rust的源代码,以便更好地熟悉该语言.我遇到了这个片段.
// Collect program arguments as a Vec<String>.
let cmd: Vec<_> = env::args().collect();
// Some unrelated code omitted here.
match subcommand::parse_name(&cmd[1][..]) {
// It did some stuff here.
}
Run Code Online (Sandbox Code Playgroud)
我不明白[..].所以,我去检查了以下声明parse_name:
pub fn parse_name(name: &str) -> Option<Box<Subcommand>>
Run Code Online (Sandbox Code Playgroud)
这是我的预期,但我仍然没有得到[..].在这种情况下它意味着什么?是不是只是路过第一String的cmd作为&str?如果是这样,这相当于写作cmd[1]吗?他们为什么这样做?
我想做的就是将单个转换Character为大写而不需要转换为a String然后调用的开销.uppercased().有没有内置的方法来做到这一点,或者我可以toupper()从C 调用函数而不需要任何桥接?我真的不认为我应该为了这么简单的事情而不顾一切.
这可能是一个快速,简单的问题,但我无法在Google上找到答案.我在C#编写了大部分编码,目前正在输入一些C,我想知道C是否也支持三次斜杠评论,或者我是否只是因为能够使用它们而被宠坏了.
谢谢!
我问的是如何在Xcode之外做到这一点.
用文本编辑器编写的Swift脚本可以用xcrun swift hello_world.swift或执行swift hello_world.swift.它们也可以设置为可执行文件并与shebang一起运行.这与Python之类的脚本语言并行.
但是,由于雨燕是编译型语言,我敢肯定,必须有使用通过终端建设斯威夫特可执行的一些方式swift,clang等有没有人发现就这事?我惊奇地发现什么都没有.