Rust 如何在read_to_字符串更改为使用缓冲区后创建链接API?
我正在尝试将我的库移植到最新版本 Rust在前一个月发生了很大变化,所以我对这段代码摸不着头脑,问自己是否真的没有办法用完全链接的方式来写这段代码了Rust 如何在read_to_字符串更改为使用缓冲区后创建链接API?,rust,chaining,Rust,Chaining,我正在尝试将我的库移植到最新版本 Rust在前一个月发生了很大变化,所以我对这段代码摸不着头脑,问自己是否真的没有办法用完全链接的方式来写这段代码了 fn get_last_commit () -> String { let output = Command::new("git") .arg("rev-parse") .arg("HEAD") .output()
fn get_last_commit () -> String {
let output = Command::new("git")
.arg("rev-parse")
.arg("HEAD")
.output()
.ok().expect("error invoking git rev-parse");
let encoded = String::from_utf8(output.stdout).ok().expect("error parsing output of git rev-parse");
encoded
}
在旧版本的Rust中,代码可以这样编写
pub fn get_last_commit () -> String {
Command::new("git")
.arg("rev-parse")
.arg("HEAD")
.spawn()
.ok().expect("failed to invoke rev-parse")
.stdout.as_mut().unwrap().read_to_string()
.ok().expect("failed to get last commit")
}
似乎再也没有read\u to\u string()
方法不需要缓冲区,这使得实现链接API非常困难,除非我遗漏了什么
更新
好的,我想我可以使用map
来链接它
fn get_last_commit () -> String {
Command::new("git")
.arg("rev-parse")
.arg("HEAD")
.output()
.map(|output| {
String::from_utf8(output.stdout).ok().expect("error reading into string")
})
.ok().expect("error invoking git rev-parse")
}
实际上,我想知道我是否可以使用
然后使用,但似乎错误排列不正确;) 此API已更改,因此您无需每次重新分配新的字符串。然而,正如您所注意到的,如果您不关心分配,就会有一些便利性损失。建议将其重新添加到中可能是一个好主意,比如Vec::from_elem
。可能打开一个小的RFC?此API已更改,因此您不必每次重新分配一个新的字符串。然而,正如您所注意到的,如果您不关心分配,就会有一些便利性损失。建议将其重新添加到中可能是一个好主意,比如Vec::from_elem
。也许可以打开一个小的RFC?虽然尝试将其添加回标准库可能是有意义的,但这里有一个版本的read\u to\u string
,它可以自己分配,您现在可以使用:
#![feature(io)]
use std::io::{self,Read,Cursor};
trait MyRead: Read {
fn read_full_string(&mut self) -> io::Result<String> {
let mut s = String::new();
let r = self.read_to_string(&mut s);
r.map(|_| s)
}
}
impl<T> MyRead for T where T: Read {}
fn main() {
let bytes = b"hello";
let mut input = Cursor::new(bytes);
let s = input.read_full_string();
println!("{}", s.unwrap());
}
#![专题(io)]
使用std::io::{self,Read,Cursor};
阅读:阅读{
fn读取完整字符串(&mut self)->io::结果{
让mut s=String::new();
设r=self.read_to_string(&mut s);
r、 地图(| | s)
}
}
T的impl MyRead,其中T:Read{}
fn main(){
让bytes=b“hello”;
让mut input=Cursor::new(字节);
设s=input.read_full_string();
println!(“{}”,s.unwrap());
}
这应该允许您使用以前的链接样式。虽然尝试将其添加回标准库可能有意义,但下面是一个版本的read\u to\u string
,它可以自己分配,您现在可以使用:
#![feature(io)]
use std::io::{self,Read,Cursor};
trait MyRead: Read {
fn read_full_string(&mut self) -> io::Result<String> {
let mut s = String::new();
let r = self.read_to_string(&mut s);
r.map(|_| s)
}
}
impl<T> MyRead for T where T: Read {}
fn main() {
let bytes = b"hello";
let mut input = Cursor::new(bytes);
let s = input.read_full_string();
println!("{}", s.unwrap());
}
#![专题(io)]
使用std::io::{self,Read,Cursor};
阅读:阅读{
fn读取完整字符串(&mut self)->io::结果{
让mut s=String::new();
设r=self.read_to_string(&mut s);
r、 地图(| | s)
}
}
T的impl MyRead,其中T:Read{}
fn main(){
让bytes=b“hello”;
让mut input=Cursor::new(字节);
设s=input.read_full_string();
println!(“{}”,s.unwrap());
}
这应该允许您使用以前的链接样式。正如其他人所说,这已更改为允许重用缓冲区/避免分配
另一种选择是使用read_to_string
并手动提供缓冲区:
pub fn get_last_commit () -> String {
let mut string = String::new();
Command::new("git")
.arg("rev-parse")
.arg("HEAD")
.spawn()
.ok().expect("failed to invoke rev-parse")
.stdout.as_mut().unwrap()
.read_to_string(&mut string)
.ok().expect("failed to get last commit");
string
}
正如其他人所说,这是为了允许重用缓冲区/避免分配
另一种选择是使用read_to_string
并手动提供缓冲区:
pub fn get_last_commit () -> String {
let mut string = String::new();
Command::new("git")
.arg("rev-parse")
.arg("HEAD")
.spawn()
.ok().expect("failed to invoke rev-parse")
.stdout.as_mut().unwrap()
.read_to_string(&mut string)
.ok().expect("failed to get last commit");
string
}
好的,我保证写一封。不确定今天是否还在,因为已经是午夜了,但我保证我会的。我还在为这件事挠头;)实际上我甚至开始写它;)这里没问题,我保证写一封。不确定今天是否还在,因为已经是午夜了,但我保证我会的。我还在为这件事挠头;)实际上我甚至开始写它;)给你