Reusable/borrowed buffer within an `Iterator`

⚓ Rust    📅 2025-08-06    👤 surdeus    👁️ 5      

surdeus

Have been trying to convert a (seemingly) straightforward loop:

let mut input = Input::new();
loop {
  // fn read_line(&mut self) -> &str
  let line = input.read_line();
  if !line.is_empty() { ... }
}

Into an Iterator to opt into a for line in input { ... }.

The simplest/most intuitive way about it seems to (necessarily) involve unsafe.

Summary (click for more details)

The closest safest alternative, on the other hand, appears to involve GATs and wrappers and lending iterators and god knows what else. Is there any safe alternative for this particular case?

2 posts - 2 participants

Read full topic

🏷️ Rust_feed