These are chat archives for rust-lang/rust

14th
Jul 2017
Andrew De Ponte
@cyphactor
Jul 14 2017 05:11
Ok, I am relatively new to Rust and I am running into an issue. The following gist shows the small snippet of code I am trying to get to compile and the error that I am getting. https://gist.github.com/cyphactor/8b6e8ac8d756a3451bad918983f3145d I would appreciate any help or direction.
I don’t understand where the [u8] is coming into play
Andrew De Ponte
@cyphactor
Jul 14 2017 05:19
nm, I found the issue rust-lang/rust#23286
RuRu92
@RuRu92
Jul 14 2017 08:24

Hey guys, need some help with understanding threads and borrowing in rust, An issue is I dont know how to avoid or manage my objects going out of scope.

Example code below. The general idea is to have a writer and write to a number container and later have a reader to read. But I am having issue to passing the container to the thread and having it alive after the thread is done.

fn main() {
    println!("Hello, world!");
    let mut holder = Container {numbers: vec![]};
    let mut writer = Writer { name: "writer".to_string(), number: 1 };
    let mut reader = Reader::new("reader".to_string(), 1);

    let mut io = (writer, reader);

    let _ = thread::spawn(move || {
        let mut container = holder.clone();

        for num in 0..20 {
            io.0.write(&mut container, num);
        };
    }).join();

    println!("data Holder: {:?}", holder.numbers);
}


#[derive(Debug, Clone)]
struct Container {
    numbers: Vec<u32>
}

#[derive(Debug, Clone)]
struct Writer {
    name: String,
    number: u32
}

//impl Copy for Writer {}

impl Writer {
    fn new(name: String, number: u32) -> Writer {
        return Writer {
            name: name,
            number: number
        }
    }

    fn write(&self, holder: &mut Container, num: u32) {
        println!("Writing number: {}", num);
        holder.numbers.push(num);
    }
}
Later on I want a reader to read and print the numbers in the container. And of course the container should be safe and block when there is no value or no new value to be read from
How should I go bout this with Rust?
Ilya Bogdanov
@vitvakatu
Jul 14 2017 08:27
I think you need to look at Arc and RwLock types.
RuRu92
@RuRu92
Jul 14 2017 08:37
Could you give some examples on how to use Arc with my own types. I have tried it, but didnt get far
Max Frai
@max-frai
Jul 14 2017 08:49
@RuRu92 Arc is just a wrapper with reference counting
Use it like box or rc
RuRu92
@RuRu92
Jul 14 2017 08:54

So I can just do this?

let mut holder = Arc::new(Container {numbers: vec![]});

  let cont = thread::spawn(move || {
        let container = holder.clone();
        for num in 0..20 {
            let io = io.0.clone();

            io.write(&mut container, num);
        };
    }).join();

But compiler complains about "cannot borrow immutable borrowed content as mutable cannot borrow as mutable"

Ilya Bogdanov
@vitvakatu
Jul 14 2017 08:57
The content of Arc is immutable, you need Mutex or RwLock to mutate it
RuRu92
@RuRu92
Jul 14 2017 10:26
So my container would be something like this? let holder = Arc::new(Mutex::new(Container {numbers: vec![]}));
lemonxah
@lemonxah
Jul 14 2017 12:55
hi guys new to rust .. so what is up with String when should you use String and when &str?
Max Frai
@max-frai
Jul 14 2017 12:56
@lemonxah The main difference is that String is heap-allocated string
So when you need ability to modify string or perform some operations on it, you should use String
Btw, https://doc.rust-lang.org/nightly/book/first-edition/strings.html it's good described in official docs
lemonxah
@lemonxah
Jul 14 2017 13:03
thank you