These are chat archives for rust-lang/rust

1st
Aug 2017
stevensonmt
@stevensonmt
Aug 01 2017 04:41

In chapter 14 of the 2nd edition of the Book, when running cargo test for the add_one example, I get the error

error[E0423]: expected function, found module add_one

The code is copy-pasted from the Book, so what's the issue?
N/M, found issue on github with solution.

lemonxah
@lemonxah
Aug 01 2017 07:46
is there a functional Traverse example library? going from Vec<Option<A>> to Option<Vec<A>>
i am trying to write one using a HKT type
pub trait HKT<B> {
    type A; // Current type
    type M; // Type with A swapped with B
}
not mine i saw this online and trying to play around it
anyone that could give me some guidence on the implementation of this? It would be a lot easier in scala :)
not just the implementation but just to double check my Types for the Traverse<B> type if ti makes sense
Sherab Giovannini
@Shaddy
Aug 01 2017 19:08
a simple question, which is the best way to put elements in front of a vector?
vector.insert(0, element)?
to just push them and iter().rev()
?
Jonas Platte
@jplatte
Aug 01 2017 19:10
@Shaddy If you want to add stuff in a loop and then iterate over it in the reverse order, push() + iter().rev() sounds like a good idea
Sherab Giovannini
@Shaddy
Aug 01 2017 19:16
I'm adding strings to vector with "push" order, but I want to join them in a reversed order
ok, thanks :)
["1", "2", "3] into "3-2-1" basically
Antonin Carette
@k0pernicus
Aug 01 2017 19:32
Got the bad news, not selected at Rust’s Reach program :-(
367 applications - pretty cool! :-)
Sherab Giovannini
@Shaddy
Aug 01 2017 20:17
11 |     println!("{:?}", vec.iter().rev().collect::<Vec<_>>().join("-"));
   |                                                           ^^^^
   |
   = note: the method `join` exists but the following trait bounds were not satisfied:
           `&&str : std::borrow::Borrow<[_]>`
           `&&str : std::borrow::Borrow<str>`
how do I let him to borrow the strings?
Max Frai
@max-frai
Aug 01 2017 20:19
@Shaddy btw, you can call reverse directly on vector
Sherab Giovannini
@Shaddy
Aug 01 2017 20:20
damn
well thats exactly what I need, anyway
just for curious, whats the problem there?
Max Frai
@max-frai
Aug 01 2017 20:23
@Shaddy what data types does vector hold?
Sherab Giovannini
@Shaddy
Aug 01 2017 20:23
&str
just a dummy test:
fn main() {
    let mut vec:Vec<&str> = Vec::new();
    vec.push("1");
    vec.push("2");
    vec.push("3");
    vec.push("4");
    println!("{:?}", vec.join("-"));
    vec.reverse();
    println!("{:?}", vec.join("-"));

    println!("{:?}", vec.iter().rev().collect::<Vec<_>>().join::<str>("-"));

}
join exists, but there is no "borrow" for string references
James McCoy
@jamessan
Aug 01 2017 20:31
You're using .iter(), which iterates over &T. Your T is &str so you're collecting into a Vec<&&str>. If you use .into_iter() instead, you transfer ownership and then have the expected Vec<&str>
Sherab Giovannini
@Shaddy
Aug 01 2017 20:38
thanks @jamessan !
great explanation
Sherab Giovannini
@Shaddy
Aug 01 2017 22:44
is there any rust native profiler?
Max Frai
@max-frai
Aug 01 2017 22:45
valgrind
Sherab Giovannini
@Shaddy
Aug 01 2017 22:45
it sounds more like a generic one, but would work guess :D
Max Frai
@max-frai
Aug 01 2017 22:45
There is no difference :)
Rust is compiled into bytecode using llvm same as c++, for example
Sherab Giovannini
@Shaddy
Aug 01 2017 22:46
yes, I know
but asking because sometimes package managers have theirs own profilers
the main thing with all those tools is that usually only works for unix based systems
Max Frai
@max-frai
Aug 01 2017 22:49
Go to the dark side :) Maybe someone else will answer your question
Sherab Giovannini
@Shaddy
Aug 01 2017 22:49
xD
I'm using an osx laptop at home, it is no problem. but mainly doing dev under windows :(