Похожие чаты

How to use Rc<BufReader<File>> ? I get error at line 8.

33 ответов

38 просмотров
Brian- Автор вопроса
Brian
screenshot

You're using Rc. Consider cloning.

let x = Rc::new(4); let _y = Rc::clone(&x);

Brian- Автор вопроса
Brian
screenshot Get this error now.

No no. Read my message correctly.

Brian
screenshot Get this error now.

I said Rc::clone(&reader) not reader.clone()

Brian- Автор вопроса

you need to get &mut BufReader<File> out of your Rc, but for that you'll need a RefCell

Brian- Автор вопроса
Brian
screenshot What am I doing wrong? 😳

(&mut *reader.borrow_mut()).lines()

Brian- Автор вопроса

What do I use on types that don't implement Copy trait?

Brian
What do I use on types that don't implement Copy t...

do you really need Rc in your example? I doubt it

Brian- Автор вопроса
Artem Starikov
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

Brian- Автор вопроса
Raymond
Try using RefCell without Rc.

I thought all RefCell does is allow to borrow mutably on immutable types

Brian
What else can I do, since I am using reader again,...

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()

Brian- Автор вопроса
Artem Starikov
Rc won't help you with this at all. yes, lines ta...

😂, What vodoo is this? can you explain more.

Brian
😂, What vodoo is this? can you explain more.

just do let reader = BufReader::new(file); and use (&mut reader).lines()

Brian- Автор вопроса
Artem Starikov
just do let reader = BufReader::new(file); and use...

Hmm... can't borrow mutably, are you sure we don't need RefCell to make that possible? otherwise make reader mut?

Brian
screenshot Hmm... can't borrow mutably, are you sure we don't...

let mut reader`is enough. you don't need `RefCell here at all

Brian
screenshot Hmm... can't borrow mutably, are you sure we don't...

You overengineered it. You don't need it there.

Brian- Автор вопроса
Artem Starikov
let mut reader`is enough. you don't need `RefCell ...

Now notice the output. it doesn't read the lines in the file anymore. 😂 The line: println!("LINE:{}",line.unwrap()); Now never runs.

Brian
screenshot Now notice the output. it doesn't read the lines i...

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

Brian- Автор вопроса
Artem Starikov
well yes, the first call to lines() read the whole...

😂😂, so would have rather read those values and stored in a Vec<String> 😂... No need for mut, thanks.

Brian
😂😂, so would have rather read those values and sto...

actually, something along these lines would work: let lines = file.lines() .inspect(|line| println!("LINE: {}", line.unwrap()) .count();

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта