These are chat archives for rust-lang/rust

10th
Sep 2016
Vincent Cantin
@green-coder
Sep 10 2016 06:10
Hi. I don't understand what this line mean exactly. Could someone explain the {self} ?
use std::io::{self};
Erik Hedvall
@Ogeon
Sep 10 2016 08:40
It's the exact same thing as use std::io;, but the self keyword is useful when you want to import both the module and something from inside it: use std::io::{self, Read, Write};
Oleg Yamnikov
@yamnikov-oleg
Sep 10 2016 09:13
Oh, nice feature to learn
Vincent Cantin
@green-coder
Sep 10 2016 09:50
Oh, I see. The author of the source file I read was probably writing it by convention, as he was using the form std::io::{self, Read, Write}; here and there. Thank you !
Erik Hedvall
@Ogeon
Sep 10 2016 10:13
use std::io::{self}; looks like it included more imports, but they were removed.
James
@avitex
Sep 10 2016 10:13

Could someone point me to some good material on how to avoid designing rust code where you require multiple mutable||immutable&&mutable references.
Eg: On a struct, I want to be able to set a member based on a matched reference in an array, who also is a member of the struct. https://is.gd/ZaMxzm

Thanks in advance.

From what I understand, I just need to start thinking the rust way, but it's a bit of a learning curve.
More food for thought, perhaps I am not separating concerns well enough? If I do need to seperate more, can I rely on the compiler to keep things tight? I could be currently held hostage by premature optimisation :smile:
Erik Hedvall
@Ogeon
Sep 10 2016 11:32
Something to keep in mind is that using getters on an object will always borrow the whole object until the reference is dropped, while accessing individual fields will just borrow the fields. One way to keep using getters is to split the content of a struct into smaller structs (it's usually free, so no worry about memory overhead) and implement the getters on them. You could, for example, have something like this:
struct Fibers {
    fibers: Vec<Fiber>,
    current_fiber_index: usize,
}

impl Fibers {
    fn get_fiber_mut(&mut self) -> Option<&mut Fiber> {
        self.fibers.get_mut(self.current_fiber_index)
    }
}

struct Program {
    instructions: Vec<Instruction>,
}

impl Program {
    fn fetch_instr(&self, index: usize)-> Option<&Instruction> {
        Some(&self.instructions[index])
    }
}

struct Sandbox {
    fibers: Fibers,
    program: Program,
}
James
@avitex
Sep 10 2016 11:39
@Ogeon Terrific, that's a great starting point. So I shouldn't have to really worry about abstracting things/seperating the concerns like this.Coming from a less strict, and more 1:1 implementation language like c to rust means changing the way I approach solving problems I guess.
Sorry if the solution seems obvious, but thank you
Viktor
@guojun1992
Sep 10 2016 12:23
hello all
mhsjlw
@mhsjlw
Sep 10 2016 13:55
hi
Erik Beran
@eberan
Sep 10 2016 17:41
Is there a way to defer struct/object initialization?
e.g. I have a big array of structs which I will initialize later
initialization here doesn't mean allocation, just assigning values