These are chat archives for rust-lang/rust

25th
Nov 2018
Kelly Thomas Kline
@kellytk
Nov 25 2018 01:36
For a u32 key why is the error at https://pastebin.com/U21ncesZ being generated please?
Tim Robinson
@1tgr
Nov 25 2018 08:51
@loloof64 with Rc and the other smart pointers, your whole program has to work with the Rc type
So your function will return Option<Rc<ChessBoard>> not Option<ChessBoard>
Also, you can call methods on a smart pointer directly, so:
chess_board_ref.drawing_area.connect_draw(|_drawing_area, cr|{
    chess_board_ref.paint(cr);
    Inhibit(false)
});
Also, the idea is that you have multiple Rcs which point back to a single ChessBoard. The Rcs have their own lifetimes that are independent of one another, and you give one to the closure and you return the other.
let chess_board_ref1 = Rc::new(chess_board);
let chess_board_ref2 = chess_board_ref1.clone();

chess_board_ref.drawing_area.connect_draw(move |_drawing_area, cr|{
    chess_board_ref1.paint(cr);
    Inhibit(false)
});

Some(chess_board_ref2)
Tim Robinson
@1tgr
Nov 25 2018 08:56
Note move on the closure, which means it owns chess_board_ref2. If the move wasn't there then it would capture a reference to chess_board_ref2 instead of owning it, and you'd be back with your original problem.
@kellytk the type of id is &u32, and you won't be able to use this reference from inside the then closure
Dereference it outside the closure not inside it
Try this:
for &id in self.sessions.keys() {
    if let Some(session) = self.sessions.get(&id) {
Kelly Thomas Kline
@kellytk
Nov 25 2018 12:11
Thanks @1tgr
dovreshef
@dovreshef
Nov 25 2018 13:34
Where can I read about the difference between macro! to macro_rules! ? I'm also looking for more in depth macros docs
David O'Connor
@David-OConnor
Nov 25 2018 13:50
Info on declarative macros. I've been unable to find documentation or tutorials on procedural macros, but the Official Rust book, and the link above have some examples of declarative macros (eg macro_rules)
dovreshef
@dovreshef
Nov 25 2018 14:08
@David-OConnor Thanks!
laurent bernabé
@loloof64
Nov 25 2018 15:42
@1tgr Thank you very much : will read all your explanations and try your advice right now :smile:
laurent bernabé
@loloof64
Nov 25 2018 15:56
Thank you again :smile:, it worked : https://pastebin.com/X233ywKw
laurent bernabé
@loloof64
Nov 25 2018 16:11
Does Rc has a method in order to borrow mutably ? When I need to call the paint() method in my snippet above, that was fine because paint borrow immutably. But this time I need to call a method that borrows mutably from a Smart Pointer :
let click_handler_chessboard = chessboard.clone();
        reverse_board_button.set_image(&reverse_board_button_image);
        reverse_board_button.connect_clicked(move |_button|{
            click_handler_chessboard.reverse();
        });
cannot borrow immutable borrowed content as mutable
  --> src/chess_position_trainer/graphic/main_window.rs:48:13
   |
48 |             click_handler_chessboard.reverse();
   |             ^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
pub fn reverse(&mut self) 
    {
        self.reversed = ! self.reversed;
    }
(last snippet is part of chessboard implementation)
rcRef.make_mut() ? Rc::make_mut(&mut chessboard) ?
laurent bernabé
@loloof64
Nov 25 2018 17:43
Finally using a RefCell instead of Rc, and calling borrow()/borrow_mut() on it
Martin Nuc
@MartinNuc
Nov 25 2018 19:05
Hey... I am new to Rust. Starting now with documentation. I am JS/TS web developer but I have C++ background from the university. I decided to learn a new language and picked Rust :-) Reason is it could be handy for webassembly. So I wanted to say hi to everyone here 👋
Kelly Thomas Kline
@kellytk
Nov 25 2018 19:17
I came to Rust from JS/TS and C as well as others before that. Welcome @MartinNuc :-)