These are chat archives for rust-lang/rust

13th
Oct 2017
TatriX
@TatriX
Oct 13 2017 08:45
What profiling tool should I use to find which parts of the program executes too long?
Aleksey Kladov
@matklad
Oct 13 2017 08:46
On linux, I had great experience with perf + flamegraphs for Rust.
TatriX
@TatriX
Oct 13 2017 08:51
Any good articles on using that?
Aleksey Kladov
@matklad
Oct 13 2017 08:52
There's http://www.brendangregg.com/perf.html, which is the definitive guide from the author, but it's way too long unfortunately :(
TatriX
@TatriX
Oct 13 2017 08:56
What about cargo profiler?
Aleksey Kladov
@matklad
Oct 13 2017 09:00
Haven't used that myself, but looks like it's using callgrind under the hood, and I had better experience with perf then callgrind (mainly because perf + flamegraphs are an excellent visualization, and visualizing callgrind info was difficult for me)/
TatriX
@TatriX
Oct 13 2017 09:21
 cargo run --release
LOAD Duration { secs: 0, nanos: 346150935 }

 cargo run
LOAD Duration { secs: 7, nanos: 663883538 }
8(((
xhallix
@xhallix
Oct 13 2017 10:30

Can anyone let me know , how to create a complex object from an array through a loop?
I'm using the json and want to build a json object from an array. this sounds trivial - but somehow I cannot figure it out

let my_vec = ["foo","bar","baz","foobar","barfoo"];

My result should be

let my_obj = object!();

my_obj ["foo"]["bar"]["baz"]["foobar"]["barfoo"];
which results in
myobj : {
  foo : {
     bar : {
        baz : {
          foobar : {
            barfoo : {}
         }
     }
    }
  }
}
Denis Lisov
@tanriol
Oct 13 2017 11:55
Well, not exactly trivial... something using serde_json?
Zakarum
@omni-viral
Oct 13 2017 12:31

@xhallix

let my_vec = ["foo","bar","baz","foobar","barfoo"];
let my_obj = {
  let mut my_obj = object!();
  for i in my_vec.iter() {
    my_obj = my_obj[i];
  }
  my_obj
};

??

xhallix
@xhallix
Oct 13 2017 12:40
@omni-viral wait a second : )
@omni-viral my_obj = my_obj[i];
| ^^^^^^^^^ the type json::JsonValue cannot be indexed by &&str
xhallix
@xhallix
Oct 13 2017 12:46
@omni-viral and when derefencing i get
error[E0507]: cannot move out of indexed content
  --> src/main.rs:58:22
   |
58 |             my_obj = my_obj[*i];
   |                      ^^^^^^^^^^ cannot move out of indexed content
so for example in js I'd do something like
var buildObj = function(arr, obj, i) {
    var out = {};
    var i = i || arr.length;
    out[arr[i-1]] = obj || {};
    if ( i > 1 ) {
        out = buildObj(arr, out, i - 1);
    }
    return out;
}
buildObj(["a","b","c","d"])
xhallix
@xhallix
Oct 13 2017 13:04
borrowing my_obj leads to
58 |             my_obj =  &my_obj[*i];
   |                       ^^^^^^^^^^^ expected enum `json::JsonValue`, found reference
   |
Cengiz Can
@cengizIO
Oct 13 2017 13:06
did we abandon bors merges?
Steve Klabnik
@steveklabnik
Oct 13 2017 13:19
?
Zakarum
@omni-viral
Oct 13 2017 13:27
@xhallix I see
fn buildObj<S: AsRef<str>, I: DoubleEndedIterator<Item=S>>(mut iter: I) -> json::Object {
  let mut obj = json::Object::new();
  while let Some(name) = iter.next_back() {
    let mut parent = json::Object::new();
    parent.insert(name.as_ref(), obj.into());
    obj = parent;
  }
  obj
}
Try this