These are chat archives for rust-lang/rust

25th
Oct 2017
Fra ns
@snarf95_twitter
Oct 25 2017 09:23
returning an iterator from a function is not really possible is it???
Zakarum
@omni-viral
Oct 25 2017 09:24
@snarf95_twitter it possible
Fra ns
@snarf95_twitter
Oct 25 2017 09:24
as a generic Iterator<Item=T>
sorry
Zakarum
@omni-viral
Oct 25 2017 09:24
As generic - no
Fra ns
@snarf95_twitter
Oct 25 2017 09:24
:/
Zakarum
@omni-viral
Oct 25 2017 09:24
But you can create MyIterator<T>
And implemet Iterator<Item=T> for MyIterator<T>
Or box it
Fra ns
@snarf95_twitter
Oct 25 2017 09:25
yeah I'm trying to clear up some code where i'm basically collecting a lot to vecs and would instead like to compose it of multiple iterators
and then one big iterator that chains them
Jonas Platte
@jplatte
Oct 25 2017 09:25
Not entirely sure what you mean by returning it "as a generic Iterator", but it you mean the common thing, then you can do it on nightly with #![feature(conservative_impl_trait)].
Fra ns
@snarf95_twitter
Oct 25 2017 09:25
I'm working on a chess ai
conservative_impl_trait looks promising
Jonas Platte
@jplatte
Oct 25 2017 09:26
there is a distinction to be made here between a type that you know but you can't / don't want to declare as being that concrete type, rather referring to it as "some type that implements Iterator<Item = T>
and the user choosing the return type of your function, within given constraints
Fra ns
@snarf95_twitter
Oct 25 2017 09:28
It's difficult adjusting to this, when you come from a C# world where this is trivial :package:
Jonas Platte
@jplatte
Oct 25 2017 09:28
The latter is possible on stable and used in a few places, e.g. str::parse and Iterator::collect
Fra ns
@snarf95_twitter
Oct 25 2017 09:28
haha the yield keyword would be nice with rust :package:
Jonas Platte
@jplatte
Oct 25 2017 09:28
the former is what conservative_impl_trait allows you to do
Oh, nightly has generators too ;)
Fra ns
@snarf95_twitter
Oct 25 2017 09:29
nice
I'm on nightly
Fra ns
@snarf95_twitter
Oct 25 2017 09:29
oh boy that's amazing gonna have a look thx @jplatte
Fra ns
@snarf95_twitter
Oct 25 2017 10:58
#![feature(conservative_impl_trait)] is a really nice feature :)
Marcel
@marcelbuesing
Oct 25 2017 12:52
how do I specify when implementing a Stream combinator, that the Items must be Cloneable ? Stream<Item = ???, Error= ...>
Chaitanya Munukutla
@munukutla
Oct 25 2017 14:08
    fn handle_client(mut stream: TcpStream) {
        // read 20 bytes at a time from stream echoing back to stream

        let mut reader = BufReader::new(stream);

        for line in reader.by_ref().lines() {
            match line {
                Ok(line) => {
                    println!("Data received: {}", line);
                    send_response(reader.into_inner());
                }
                Err(error) => {
                    println!("Error reading line: {:?}", error);
                }
            }
        }
    }

    fn send_response(mut stream: TcpStream) {
        // Write the header and the html body
        let response = "Roger that";
        stream.write_all(response.as_bytes()).unwrap();
    }
I'm trying to write a simple TCP server
Compile fails because value moved here in previous iteration of loop
The error happens at the send_response(reader.into_inner());
Any idea how to make it work?
I just want to send back a reply whenever he hits an enter key
Nikita
@nikita-y
Oct 25 2017 14:48
Rust allows only one mutable borrowing
Chaitanya Munukutla
@munukutla
Oct 25 2017 16:27
Thanks a lot @nikita-y
Jonas Platte
@jplatte
Oct 25 2017 16:45
@marcelbuesing You just add that bound to the item type.
So if you have for<T> Stream<Item = T, Error = ...>, you make it for<T: Clone> Stream<Item = T, Error = ...>
does that make sense?
Nikita
@nikita-y
Oct 25 2017 17:19
Mutable borrowing &mut T is basically passing by pointer in c++ T*. Which is in general not a good idea (in c++). Because the function can pass the pointer to an another function and so forth. So if something goes wrong you have to go though whole chain to find where your data was messed up. That's probably why rust limits you by one mutable borrowing.
Chaitanya Munukutla
@munukutla
Oct 25 2017 17:43
Yeah got that. Will try a workaround and see if that works.
Jonas Platte
@jplatte
Oct 25 2017 18:37

@munukutla You're reading from and writing to the same stream in your example. I'm pretty sure that's not how you do TCP in rust.

EDIT: Sorry, I looked at the docs, was wrong.
EDIT2: I played around with it a bit and actually all that you need to change is to use get_mut() on your BufReader instead of into_inner, and then change your send_response function accordingly (take the TcpStream by &mut instead of by value)

Moggers
@Moggers
Oct 25 2017 18:50
I find it so hard to write other languages now
I go to other languages and try to do things and its just like "thats impossible"
David Harvey-Macaulay
@alteous
Oct 25 2017 18:51
^^ :laughing: