These are chat archives for rust-lang/rust

12th
Feb 2019
Joseph Lenton
@joe-askattest
Feb 12 07:33
Hello, I'm trying to build my own trait that wraps an Iterator, so it will return values as (current, next). I've done it by wrapping an underlying Peekable, but I am getting an issue with a lifetime that I'm unable to resolve. I have a gist here: https://gist.github.com/joe-askattest/46e6537d03e931fc942fcddd4499d3b5
Zakarum
@omni-viral
Feb 12 07:49
Is dyn Trait<T> covariant, contravariant or invariant over T?
Ingvar Stepanyan
@RReverser
Feb 12 13:13
@joe-askattest I don't think you can return Option<&I::Item> because Item associated type is defined without a lifetime
Denis Lisov
@tanriol
Feb 12 13:16
You cannot do that because for any finite iterator the user can do .collect::Vec<_>() while you won't be able to return your items both by reference and, after that, by value.
Fixer
@DivineSymmetry_gitlab
Feb 12 13:19
Is there some site where all prog languages are listed and compared, showing overlapping use-cases so I can understand what rust is good for, what elixir is good for, which languages they replace because they are superior, and what makes a language good for something and bad for something else, or do i have to read one wiki or whatever at a time, about "functional prog langs" "dynamic langs" etc.
If I want to learn python, rust, elixir, what does that make me? what if i add mysql, R, C? or is this not realistic, because the time it takes to maintain or even acquire deep knowledge is longer than the turnover by which I mean the time it takes for them to get revamped and become substantially different.
Denis Lisov
@tanriol
Feb 12 13:23
"all programming languages" is just too many :-)
Tim Robinson
@1tgr
Feb 12 13:24
You have to read one wiki page at a time, or ideally learn and use one language at a time
toxicafunk
@toxicafunk
Feb 12 13:24
@DivineSymmetry_gitlab u can learn them and u should try to learn as many as possible, it broadens your understanding of PL and improves your abstract thinking
but you will ned to prioritize the order
and u may want/need to specialize in one or another
Tim Robinson
@1tgr
Feb 12 13:25
There are lots of dimensions to a language, a summary would scratch the surface on things like syntax, but you also need to consider tools, libraries and support from the language community
toxicafunk
@toxicafunk
Feb 12 13:25
I suggest u find some forum where they discuss PLs and ask there as a good starting point
Tim Robinson
@1tgr
Feb 12 13:25
"What language should I learn, Mandarin or French?"
toxicafunk
@toxicafunk
Feb 12 13:25
/r/programming or some IRC or slack channel
Denis Lisov
@tanriol
Feb 12 13:26
As a better option, you should think about your domains / areas of interest and search for languages that are either applicable in some way, or that are known to teach something really well.
Fixer
@DivineSymmetry_gitlab
Feb 12 13:27
Or is it best to try to do brainstorming for a while, like what do I want to do with my life -> why and let's say that it's start up related or I just want independence and freedom and cs/prog is the way to go at least a good skill-set due to the leverage it gives me. -> come up with a product/service and THEN choose by asking people for advice what language, framework and whatever else is in a tech stack, is good for the idea I'm trying to manifest into the world?
toxicafunk
@toxicafunk
Feb 12 13:28
i'd start by asking why u want to learn programming for?
Denis Lisov
@tanriol
Feb 12 13:37
In the initial phase it's a good idea to try various languages just to understand for yourself which concepts and paradigms work for you and which... not really. Try to grasp the very basics of them. Languages like Rust ("what matters on the low level"), Python ("what if everything tries to be intuitive"), Haskell ("what the pure functional people are talking about"), Prolog ("say what you need, not how"), maybe Java ("what if everything has to be an object"), maybe some kind of assembly ("what your CPU really works with"). Someone would probably add Lisp ("code is data, data is code") or, for example, GLSL ("working in batches, don't break the execution wave"). Don't expect this to be the language you'll use most... just one of the sources of inspiration.
Fixer
@DivineSymmetry_gitlab
Feb 12 14:14

Can you guys state what the rationale for doing what you do or why you learned it is/was?

Not sure this is even all that important, since so many go into CS with just curiosity, or because they like spending a lot of time in front of their computer. Some become code monkeys some get curious->obsessed about something and ride that deep connection. Some prominent figures in society going even so far as to say that everyone should learn CS & programming.

This might all be just over thinking, maybe better to just try it all, try EdX courses, like cs50, intro to comp-sci with python (mit) etc. All the while churning out long term ideas pertaining to the incessant 'why'. Part of the problem is I have no grasp on the metrics in terms of how much time it takes to learn a given language, whether or not it's worth to start at where everyone else seems to be, IE html, js, css. Due to it being the fastest way to have some ROI in terms of money and gaining some work experience. Even though I'm more interested in say what seems to be uncertain things; Quantum computers and Blockchain. MRI technology which could image what seems to be mental/inner perception, but what the hell do I know, not even being a noob, so all I got is questions/curiosity, but I shouldn't squelch it neither, so I'm just throwing the nets out.

toxicafunk
@toxicafunk
Feb 12 14:18
common languages for blockchains are go, scala, c++ and now rust, so maybe u should start there
Zakarum
@omni-viral
Feb 12 14:19
Learning almost any particular PL is easy. It just a bunch of syntax constructions.
Learning programming is hard. It's what you do to solve problems. The harder the problem you have the harder is to solve it.
But good thing is that when you learn programming using one PL the experience is transferable
So if you can create some service on Rust - you can do the same on any language. You'd have just learn what this new language offers to you
Of course you can't become fluent in new PL overnight
And some PLs have just more concepts to learn than the others
Like, for comparison, C++ and JS
Zakarum
@omni-viral
Feb 12 14:24
The first one is overcomplicated pile of features.
The second one is easy, but not powerful.
Still. You can start writing something on C++ or JS or whatever in no time if you know programming in general
Except that with C++ you will spend 90% of time looking for what causes that segfault
And here is where Rust shines, no segfaults in safe Rust (by design)
Ichoran
@Ichoran
Feb 12 15:42
JS won't segfault; it will just keep running no matter how wrong it is :joy:
octave99
@octave99
Feb 12 15:44
Hi, I am using std::process::Command with spawn to run a process in the background. If there a possibility to make the child process exit when parent completes or is killed?
Ingvar Stepanyan
@RReverser
Feb 12 15:52
that's what should happen by default
Zakarum
@omni-viral
Feb 12 15:52
@Ichoran And eat 100% of your CPU, no matter how small the task it solves
Ingvar Stepanyan
@RReverser
Feb 12 15:52
since it's a child process
octave99
@octave99
Feb 12 16:16
@RReverser But its not happening that way. The parent completes and the child is still visible in the process list.
Command::new(DAEMON_PATH)
        .args(&["-a", "-i", &input])
        .spawn()
        .and_then(|child| {
          debug!("Daemon started with pid: {}", child.id());
          Ok(child)
        })
David O'Connor
@David-OConnor
Feb 12 17:42

Question re closures and FnMuts: I'm trying to use this fn, but am not sure how to do it. It's really an abstraction of an iterator. My attempt:

    el.get_attribute_names().map(
        |(attr_name, i)| {
           // use attr_name here
            }
        }
    );

Error:

^ expected mutable reference, found closure
   Note: expected type `&mut dyn std::ops::FnMut(wasm_bindgen::JsValue, u32, js_sys::Array) -> wasm_bindgen::JsValue`
               found type `[closure@C:\Users\david\code\seed\src\websys_bridge.rs:423:9: 427:10 el_ws:_, attrs:_]`

This is prumably an issue of incorrect syntax. How would you use a func like that above to loop?

Zakarum
@omni-viral
Feb 12 17:53
What map takes?
From error message I can assume it takes &mut dyn FnMut(...) -> ...
If that's so you should pub &mut |(attr_name, i)| { ... } there
Martijn Bakker
@Gladdy
Feb 12 18:33

I'm running into some issues with compile-time dispatch of generic traits:

use std::fmt::Debug;

trait Handler<M : Debug> {
    fn handle(m:M);
}

struct Struct;
impl Handler<String> for Struct {
    fn handle(m : String) {
        println!("got a string!");
    }
}

impl<M:Debug> Handler<M> for Struct {
    fn handle(m : M) {
        println!("got something else: {:?}", m);
    }
}

fn main() {
    let s = Struct;
    s.handle("hello");
    s.handle(5.0)
}

Rust does not seem to be able to prioritize one implementation of the trait over another?

I'm trying to use this to have some messages handled by an outer struct, and if not present, it falls back to the generic handler which passes them on to an inner struct.
such that you do not have to repeat the impls for the common Ms for every object
Ingvar Stepanyan
@RReverser
Feb 12 21:10
@octave99 That's... odd.
On another hand, depends what you're calling. Could it be that that process just starts the actual daemon service in background?
Denis Lisov
@tanriol
Feb 12 21:59
@Gladdy Correct, Rust does not try to choose the "best" implementation. There's an unstable and partially usable feature for that, but I'd not use it for now.
Ichoran
@Ichoran
Feb 12 22:18
(Hopefully the feature will stabilize soon; Scala infers the correct typeclass in cases like this, and it really simplifies getting both general and specific capabilities to work with a minimum of fuss.)