Похожие чаты

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

33 ответов

31 просмотр
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();

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

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

30500 за редактор? )
Владимир
47
any reference of this implementation?
BitBuddha
29
Ⓐrtto, [4/23/24 7:02 PM] Please explain more fully how it is not working exactly, and what are the steps you are taking, and what error messages come or what happens. Ⓐrtto, ...
Ezza Kezza
2
sounds like people have lost their kaspa on tradeogre... does this mean tradeogre not trustworthy?
Ezza Kezza
15
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
Недавно Google Project Zero нашёл багу в SQLite с помощью LLM, о чём достаточно было шумно в определённых интернетах, которые сопровождались рассказами, что скоро всех "ибешни...
Alex Sherbakov
5
So much speculation in the last week. So much volatility in price. This is because Hedera has a GC that isn't using the network it's governing. Why aren't people asking why a...
Summit Seeker R
8
Anyone else having this error when trying to make transactions?
Datzel
11
Question: How viable is it to use Anvil as the backend infrastructure for managing a TradFi portfolio, while integrating Flexa for instant liquidity and payment solutions? Cou...
Kevin
2
вы делали что-то подобное и как? может есть либы готовые? увидел картинку нокода, где всё линиями соединено и стало интересно попробовать то же в ddl на lua сделать. решил с ч...
Victor
8
Карта сайта