These are chat archives for rust-lang/rust

9th
Nov 2016
David Harvey-Macaulay
@alteous
Nov 09 2016 16:20

Hello, I need to obtain a non-trivial slice and I'm not sure how to go about it. Below is an outline of the problem:

struct Foo {
    bar: String,
    baz: u32,
}

fn main() {
    let data = vec![Foo { bar: "abc".to_string(), baz: 123 }];
    let slice: &[(&str, u32)] = // ???
}

Does anybody know of a way to do this (if it's even possible!)?

Sergey Noskov
@Albibek
Nov 09 2016 16:28
@Alteous, if you just want a slice instead of vec, you can use data.as_slice(), if you want to pattern match it, you have to match against &[Foo{bar:b, baz: u}], not against tuple, of course iterating over it first
David Harvey-Macaulay
@alteous
Nov 09 2016 16:55
fn main() {
    let data = vec![("abc".to_string(), 123)];
    let tmp: Vec<(&str, u32)> = data
        .iter()
        .map(|&(ref string, number)| (string.as_str(), number))
        .collect();
    let slice: &[(&str, u32)] = tmp.as_slice();
    println!("{:?}", slice);
}
This seems to do the job. It requires an allocation which might not be necessary, but for now it will suffice.
Maciej Hirsz
@maciejhirsz
Nov 09 2016 16:57
Do you actually need a slice there? Wouldn’t an iterator suffice, or a &[Foo]?
or &[(String, u32)] in your later example
David Harvey-Macaulay
@alteous
Nov 09 2016 16:58
@maciejhirsz No, I really need a slice. It's to interact with a third-party API.
Maciej Hirsz
@maciejhirsz
Nov 09 2016 16:58
ah
is the data dynamic?
if “abc” is a predefined in the code, you can just pass around static slices
David Harvey-Macaulay
@alteous
Nov 09 2016 17:01
No it's dynamic!
Maciej Hirsz
@maciejhirsz
Nov 09 2016 17:01
well then :)
David Harvey-Macaulay
@alteous
Nov 09 2016 17:01
And this slice will need to be generated every frame.
It's probably not a big deal though. I could keep a static mutable vector in the render loop just for this purpose.
Maciej Hirsz
@maciejhirsz
Nov 09 2016 17:03
if you can guarantee that the slices don’t outlive the frame, yeah
David Harvey-Macaulay
@alteous
Nov 09 2016 17:03
The compiler might even optimise it out .
Maciej Hirsz
@maciejhirsz
Nov 09 2016 17:04
are the strings ever built dynamically, or are you choosing one from a limited list of options?
keep in mind that both Vec and String allocate
David Harvey-Macaulay
@alteous
Nov 09 2016 17:05
Everything is dynamic, my goal is to render glTF assets using gfx.
Maciej Hirsz
@maciejhirsz
Nov 09 2016 17:07
you can go a long way using just &’static str even with very dynamic structures
but I am quite a bit out of my element, the problem arises when you need to construct the string on runtime (from user input or otherwise)
David Harvey-Macaulay
@alteous
Nov 09 2016 17:11
I'm not sure how to use &'static str in this context. Is that not just for data baked into the binary? I appreciate your input, even if you are out of your element. :)
Maciej Hirsz
@maciejhirsz
Nov 09 2016 17:21
any time you write "abc" in code you create a &'static str
and since it’s a static slice you can pass it around, put it on structs, vectors and what not without having to worry about the borrowck
David Harvey-Macaulay
@alteous
Nov 09 2016 17:25
That makes sense. I can't take advantage of this however because the data is user-provided.
andreimircescu
@andreimircescu
Nov 09 2016 23:46
how do i create a slice out of a string with a dynamic index
the rust way ?
andreimircescu
@andreimircescu
Nov 09 2016 23:52
actually i was using the wrong type