let x = Rc::new(4); let _y = Rc::clone(&x);
No no. Read my message correctly.
I said Rc::clone(&reader) not reader.clone()
you need to get &mut BufReader<File> out of your Rc, but for that you'll need a RefCell
Runtime borrow checker. Lol
(&mut *reader.borrow_mut()).lines()
What do I use on types that don't implement Copy trait?
It's not the case
do you really need Rc in your example? I doubt it
What else can I do, since I am using reader again, and reader originally is BufReader<File> which has no Copy trait
Try using RefCell without Rc.
I thought all RefCell does is allow to borrow mutably on immutable types
Rc won't help you with this at all. yes, lines takes BufReader by Self, however, the Read trait is implemented for &mut T where T: Read, so you only need to make Self == &mut BufReader. the way you do this is by (&mut buf_reader).lines()
😂, What vodoo is this? can you explain more.
just do let reader = BufReader::new(file); and use (&mut reader).lines()
Yup. You overthinked on that.
Hmm... can't borrow mutably, are you sure we don't need RefCell to make that possible? otherwise make reader mut?
let mut reader`is enough. you don't need `RefCell here at all
You overengineered it. You don't need it there.
Now notice the output. it doesn't read the lines in the file anymore. 😂 The line: println!("LINE:{}",line.unwrap()); Now never runs.
well yes, the first call to lines() read the whole file, so the second call to lines() cannot read any lines past the file end
😂😂, so would have rather read those values and stored in a Vec<String> 😂... No need for mut, thanks.
actually, something along these lines would work: let lines = file.lines() .inspect(|line| println!("LINE: {}", line.unwrap()) .count();
Обсуждают сегодня