These are chat archives for rust-lang/rust

24th
Oct 2018
Zakarum
@omni-viral
Oct 24 2018 08:57
BTW. You should be able to write iter().map(...).rev() as well with exactly same result
Denis Lisov
@tanriol
Oct 24 2018 08:58
Unless the closure in map uses mutable state...
Denis Lisov
@tanriol
Oct 24 2018 09:45
Yeah, a thinko, sorry.
Chaitanya Munukutla
@munukutla
Oct 24 2018 10:35

`pub struct Store<T: Sized> {
items: Vec<Item<T>>,
}

impl<T: Sized> Store<T> {
fn new() -> Store<T> {
return Store { items: Vec::new() };
}

fn get_item(&self, request: GetRequest) -> Option<Item<T>> {
    for item in (&self).items.iter() {
        if item.key == request.required_key {
            return Some(item);
        }
    }
    return None;
}

}`

Anyone can help me why return Some(item); | ^^^^ expected structmodel::item::Item, found reference is thrown?
Ichoran
@Ichoran
Oct 24 2018 10:45
You didn't include a runnable example so I'm not sure exactly why you're seeing that error; I get a different one. But overall it can't work the way you are trying because you get a reference to self and are trying to move out an actual item from inside it. You can't do that; you're only borrowing.
So you could return Option<&Item<T>> if you want to borrow the item from within the list.
Or you could take self and use self.items.into_iter() which moves everything out, and then you could have Option<Item<T>>
Or if Item was Clone you could keep it the same but return Some(item.clone())
rsb007
@rsb007
Oct 24 2018 11:55
which is best ide for rust
Zakarum
@omni-viral
Oct 24 2018 11:55
xi :+1:
rsb007
@rsb007
Oct 24 2018 11:56
xi ? what is this
rsb007
@rsb007
Oct 24 2018 11:57
any alternative ?\
Otto Chrons
@ochrons
Oct 24 2018 11:58
VSCode and IntelliJ IDEA
Zakarum
@omni-viral
Oct 24 2018 11:58
I use vscode
Dmitriy
@dpogretskiy
Oct 24 2018 11:58
vscode, idea, atom shenanigans, visual studio if you like pain, vim if you're a bit old school, emacs if you're an octopus and so on
btw, idea has it's own backend for compiler and stuff, all others use rls in some way of form, so pretty equivalent
rsb007
@rsb007
Oct 24 2018 12:11
thanks
Chaitanya Munukutla
@munukutla
Oct 24 2018 12:20
@Ichoran Thanks man. Worked like a charm :)
Sebastian
@sertel
Oct 24 2018 13:15

Hi! I’m trying to understand the concept of associated types a little bit better and as an exercise I’m trying to define the trait for a Functor. In Haskell this is:

class Functor f where
  fmap :: (a -> b) -> f a -> f b

Ideally, I would write in Rust:

trait Functor<A,B> {
  fn fmap(Self<A>, f:Fn(A) -> B) -> Self<B>;
}

But there are various problems with this:

  • I can not parameterize Self.
  • Self can not be a return type.

So far, I came up with this solution (which compiles):

trait FT<T> { }

trait Functor<A,B> {
    type FOut : FT<B>;
    fn fmap(self, Fn(A) -> B) -> Self::FOut;
}

But this does not reflect the fmap definition from Haskell in various aspects:

  • It is unclear that self is actually a Functor.
  • An implementation can easily return another type that satisfies the trait bound FT.

Any help on this would be highly appreciated.
How do I define a parameterized Self-type correctly such that I can even use it as a return type?
(I read these blog post http://smallcultfollowing.com/babysteps/blog/2016/11/02/associated-type-constructors-part-1-basic-concepts-and-introduction/ but they did not provide me with an answer.)

red75prime
@red75prime
Oct 24 2018 13:28
There's no first-class type constructors in Rust, so fmap cannot be implemented fully. Your implementation is not that far from to what can be done: https://gist.github.com/babygau/41d80225c4bb2acd6c6f
Sebastian
@sertel
Oct 24 2018 13:37
I somehow thought that this was the point of the associated type constructors.
tandrysyawaludin
@tandrysyawaludin
Oct 24 2018 15:49
how to run Rust on background server?
Claudeson Azurin
@ClaudeTheCazur
Oct 24 2018 16:33

receiving the following error:

the trait bound `u8: std::convert::From<rust::MutableHandle<'_, u8>>` is not satisfied

When trying to build servo with the following command
./mach build -j1 -d --verbose

I've searched the issue log on the servo repo and nothing relating to this problem seems to stand out. Any suggestions would be greatly appreciated

D'Silva
@evnix
Oct 24 2018 19:59
how do i return an error?
some_func() -> Result<String, Box<Error>> {
    if(someError()){
      return Err("Does not work");
     // how do i return an error? the above line doesn't compile
   }else{
      return Ok("works")
    }
}
Lyle Mantooth
@IslandUsurper
Oct 24 2018 20:03
@evnix , same as any struct. If it impl From<&str>, you can do return Err(Error::from("Does not work"));
If that's failure::Error, you can use Err(format_err!("Does work.")). If it's the std::error::Error trait, then you need to construct something that impls Error
Oh, well, with a Box::new() around the error (inside the Err), since that's what your function signature says.
D'Silva
@evnix
Oct 24 2018 20:10
Err(Box::new("some error")) gives: std::error::Error is not implemented for std::string::String
Lyle Mantooth
@IslandUsurper
Oct 24 2018 20:14
Yes. You do need an actual Error to put in the Box. You need to make a type that has impl Error, or use one from std: https://doc.rust-lang.org/std/error/trait.Error.html#implementors
D'Silva
@evnix
Oct 24 2018 20:15
Ah! Thanks
Lyle Mantooth
@IslandUsurper
Oct 24 2018 20:16
I like the failure crate, though. It has an Error struct that you can create, or a Fail trait that you can implement for your own error types in various ways.
D'Silva
@evnix
Oct 24 2018 20:18
@IslandUsurper Thanks I'll look into the crate.
Ryan
@rnleach
Oct 24 2018 21:36
What happens when I try to send to a crossbeam_channel that has been closed due to returning already?