반복자에서 데이터의 서식을 지정하는 방법을 만들고 있습니다. 허용하려면 체인, 내가 제네릭을 통해 반복자의 새로운 방법으로 제공하기 위해 노력하고있어 : 다음모든 반복자에 대한 특성 구현
trait ToSeparatedString {
fn to_separated_string(self, line_prefix: &str, separator: &str) -> String;
}
impl<T, I> ToSeparatedString for I
where
T: Display,
I: Iterator<Item = T> + Clone,
{
fn to_separated_string(self, line_prefix: &str, separator: &str) -> String {
let len = self.clone().count();
self.enumerate()
.map(|(i, line)| if i < len - 1 {
(line, separator)
} else {
(line, "")
})
.fold::<String, _>("".to_owned(), |acc, (line, line_end)| {
format!("{}{}{}{}", acc, line_prefix, line, line_end)
})
}
}
내가 여기를 사용하고 있습니다 : 난 후 그것을 사용하고 때
#[derive(Debug)]
pub struct TransactionDocumentBuilder<'a> {
/// Currency Id.
pub currency: &'a str,
/// Document timestamp.
pub blockstamp: Blockstamp,
/// Transaction locktime (in seconds ?)
pub locktime: u64,
/// List of issuers.
pub issuers: Vec<ed25519::PublicKey>,
/// List of inputs.
pub inputs: Vec<Input>,
/// List of outputs.
pub outputs: Vec<Output>,
/// Transaction comment.
pub comment: &'a str,
}
impl<'a> DocumentBuilder<TransactionDocument> for TransactionDocumentBuilder<'a> {
fn build_with_signature(self, signature: ed25519::Signature) -> TransactionDocument {
TransactionDocument {
document: GenericDocumentBuilder::new(10, "Transaction", self.currency)
.with("Blockstamp", &self.blockstamp.to_string())
.with("Locktime", &self.locktime.to_string())
.with("Issuers", &self.issuers.iter().to_separated_string("", "\n"))
.with("Inputs", &self.inputs.iter()
.map(|input| input.source)
.to_separated_string("", " "))
// Iterate through each input unlocks
.with("Unlocks", &self.inputs.iter()
.enumerate()
.map(|(i, input)| {
input.unlocks.iter().to_separated_string(&i.to_string(), "\n")
})
.to_separated_string("", "\n")
)
// more fields
.build_with_signature(signature),
};
unimplemented!()
}
fn build_and_sign(self, _private_key: &ed25519::PrivateKey) -> TransactionDocument {
unimplemented!()
}
}
이 작품의 .iter()
인데 .map()
이후는 아니며 구현되지 않았다고 말합니다. 하지만 std::slice::Iter
및 std::iter::Map
은 Iterator<Item = T> + Clone
을 구현하므로 문제가 어디에 있습니까?
도움을 주셔서 감사합니다.
같은
to_separated_string
를 다시 작성하여Clone
에 대한 요구 사항을 제거 할 수 있습니다. co.kr/help/mcve)를 사용하면 더 나은 답변을 얻을 수 있습니다. – red75prime나는 몰랐다. 나는 미래의 질문을 염두에 두겠다. – Nanocryk