मुझे एक चरित्र धारा में आगे बढ़ने में दिलचस्पी है। मेरी समझ के लिए, Peekable जाने का रास्ता होगा। मैं नहीं कर सकता इसका उपयोग कैसे करें इसका पता लगाएं।जंग की चोटी का उपयोग कैसे करें?
प्रथम प्रयास:
fn trawl<I, E>(pk: &mut I) where I: std::iter::Peekable<Result<char, E>> {
loop {
let cur = pk.next();
let nxt = pk.peek();
match (cur, nxt) {
(Some(i), Some(nxt_i)) => println!("{} {}", i.ok(), nxt_i.ok()),
_ =>(),
}
}
}
fn main() {
trawl(&mut std::io::stdio::stdin().chars());
}
इस के साथ
> rustc /tmp/main.rs
/tmp/main.rs:1:37: 1:73 error: `std::iter::Peekable` is not a trait
/tmp/main.rs:1 fn trawl<I, E>(pk: &mut I) where I: std::iter::Peekable<Result<char, E>> {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
ठीक है, पर्याप्त उचित संकलित करने के लिए विफल रहता है। मैं पूरी तरह से लक्षण समझ में नहीं आता अभी तक तो मैं में एक इटरेटर पारित करने के लिए और फिर एक peekable संस्करण बनाने की कोशिश: साथ
fn trawl<I, E>(it: &mut I) where I: Iterator<Result<char, E>> {
let mut pk = it.peekable();
loop {
let cur = pk.next();
let nxt = pk.peek();
match (cur, nxt) {
(Some(i), Some(nxt_i)) => println!("{} {}", i.ok(), nxt_i.ok()),
_ =>(),
}
}
}
fn main() {
trawl(&mut std::io::stdio::stdin().chars().peekable());
}
यह विफल रहता है
> rustc /tmp/main.rs
/tmp/main.rs:2:18: 2:20 error: cannot move out of dereference of `&mut`-pointer
/tmp/main.rs:2 let mut pk = it.peekable();
^~
/tmp/main.rs:7:65: 7:70 error: cannot move out of dereference of `&`-pointer
/tmp/main.rs:7 (Some(i), Some(nxt_i)) => println!("{} {}", i.ok(), nxt_i.ok()),
^~~~~
note: in expansion of format_args!
<std macros>:2:23: 2:77 note: expansion site
<std macros>:1:1: 3:2 note: in expansion of println!
/tmp/main.rs:7:39: 7:77 note: expansion site
error: aborting due to 2 previous errors
कोई समझा सकते हैं:
- क्यों पेकटेबल एक विशेषता होने की कमी के लिए फ़ंक्शन प्रकार में दिखाई नहीं दे सका,
- कंपाइलर का क्या मतलब है जब 'कहां से बाहर निकलना' और
- मैं या तो दोनों को कैसे हल कर सकता हूं?
एक तीसरे संस्करण
fn trawl<I, E>(mut it: I) where I: Iterator<Result<char, E>> {
let mut pk = it.peekable();
loop {
let cur = pk.next();
let nxt = pk.peek();
match (cur, nxt) {
(Some(i), Some(nxt_i)) => println!("{} {}", i.ok(), nxt_i.ok()),
// (Some(i),) => println!("{}", i.ok()),
_ =>(),
}
}
}
fn main() {
trawl(std::io::stdio::stdin().chars().peekable());
}
यह विफल रहता है के साथ: मैं
> rustc /tmp/main.rs
/tmp/main.rs:7:65: 7:70 error: cannot move out of dereference of `&`-pointer
/tmp/main.rs:7 (Some(i), Some(nxt_i)) => println!("{} {}", i.ok(), nxt_i.ok()),
^~~~~
note: in expansion of format_args!
<std macros>:2:23: 2:77 note: expansion site
<std macros>:1:1: 3:2 note: in expansion of println!
/tmp/main.rs:7:39: 7:77 note: expansion site
error: aborting due to previous error
समझने के लिए जंग मेरे लिए कह रहा है यहाँ, कैसे Iterator.next एक अलग होता है असफल Peekable.peek से वापसी प्रकार।
मुझे पता नहीं था कि stdin के बारे में। धन्यवाद। – troutwine
भावी पाठकों के लिए नोट: जब यह उत्तर लिखा गया था, ['stdin()' सुरक्षित रूप से उपयोग करना मुश्किल था] (https://github.com/rust-lang/rust/issues/14434)। यह अब मामला नहीं है, हालांकि, दिसंबर 2014 में यह मुद्दा तय किया गया था। – kiwidrew