Похожие чаты

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

33 ответов

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

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

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

А как старый хаскел с новым стыковать ? потому как тут работает https://play.haskell.org/saved/C3xpMzcd, а вот тут https://stepik.org/lesson/7602/step/9?unit=1473 нет ошибка C...
Fedor
131
что насчет пагинга? на осдеве непонятно(
Vi Chapmann 🪙
26
Guys what is ur real (no jokes) expectation of link price during this cycle?
Hobbyist
28
hey everyone I'm not able to input come characters in my flatpak apps, they get ignored by the application. all other non-flatpak apps are fine with these characters. I search...
elo
20
>>> Failed to install app-alternatives/awk-4, Log file: >>> '/var/tmp/portage/app-alternatives/awk-4/temp/build.log' * Messages for package app-alternatives/awk-4: * Pa...
[cocoa] wizzy hell
1
Вопрос я правильно понимаю что в коде newtype ArrowMap k v = ArrowMap { getArrowMap :: k -> Maybe v } getArrowMap есть функция типа k -> Maybe v, если да, то не понимаю задач...
Fedor
64
Hey everyone! How's your weekend going so far?
Noah Noure
17
Hey, it seems like a solid project for the gaming industry. What are your initial thoughts?
Josh
26
Ребят, что лучше для реверса: гидра или ида?
En Vind Av Sorg
26
Делаю велосипед логгер. К сообщению хочу прикрутить некоторую информацию, типа, кем отправлено, какой уровень, и всякое такое. И тут подумалось мне, почему бы не хранить весь...
Serjone
24
Карта сайта