Похожие чаты

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

33 ответов

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

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

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

А еще в перле можно уже @arr1 + @arr2?
Sergei Zhmylove
53
я не магистр хаскеля, но разве не может лейзи тип конвертнуться в не-лейзи запросив вычисление содержимого прям при инициализации?
deadgnom32 λ madao
100
Подскажите, где смотреть результат выполнения программы? Код: ;.686 ;Система команд процессора 686 ;.MODEL FLAT,stdcall ;Модель памяти плоская, станда...
Егор Анелькин
5
I arrived here after a Chico Crypto show highlighted the project & the Team - the fact that the Team had a long history of successfully working with household names gave me e...
Banter is Bullish
1
Привет всем. появился вопрос. Разрабатываю сайт, в данный момент он запущен. Хостинг beget. Добавляю на сайт яндекс метрику с помощью полей client-settings (взято отсюда http...
Andrew
2
Добрый день подскажите пожалуйста может кто то сталкивался с ошибками Sentry 22.10.0 развернутым из helm чарт в Kubernetes? Изначально 3 дня назад очень стало много событий ух...
Tire4 Finist Devops
1
;.686 ;Система команд процессора 686 ;.MODEL FLAT,stdcall ;Модель памяти плоская, стандартный ;вызов процедуры ;option casemap:no...
Егор Анелькин
1
Почему у макбука экран не сенсорный?
✨ Anatolii Cherednichenko
28
почому оно не работает?
Vi Chapmann Chapmann
19
It's unfortunate that the "Then why are you here" or "lol you must've bought high" mantras always come up. I already tried bringing up this subject in the price channel but si...
Leroy Gerrits
4
Карта сайта