# How to convert a Vec\<Duration\> to Vec\<(&str, u64)\>?

**URL:** <https://users.rust-lang.org/t/how-to-convert-a-vec-duration-to-vec-str-u64/57378>\
**Category:** help\
**Created:** [March 24, 2021, 3:28pm UTC](https://users.rust-lang.org/t/how-to-convert-a-vec-duration-to-vec-str-u64/57378 "2021-03-24T15:28:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhanusek](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mhanusek/32/14096_2.png) [@mhanusek](https://users.rust-lang.org/u/mhanusek)\
**Post date:** [March 24, 2021, 3:28pm UTC](https://users.rust-lang.org/t/how-to-convert-a-vec-duration-to-vec-str-u64/57378/1 "2021-03-24T15:28:22Z")

</div>

Hello.  
How to convert a `Vec<Duration>` to `Vec<(&str, u64)>`?

Please help.

```rust
    let _v: Vec<(&str, u64)> = m.freq_map().iter().map(|(i,v)|
    {
        let x = &i.as_millis().to_string() as &str;
        let z = v.clone();
        (x, z)
    }).collect();

```

> **[Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1b7cbbc2e3efcfd866125d6279b25bc4)**
>
> A browser interface to the Rust compiler to experiment with the language

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [March 24, 2021, 3:30pm UTC](https://users.rust-lang.org/t/how-to-convert-a-vec-duration-to-vec-str-u64/57378/2 "2021-03-24T15:30:22Z")

</div>

The correct type for it is `Vec<(String, u64)>` or `Vec<(Box<str>, u64>)`.

`&str` means "this is not a string, it has no data, I'm giving you a temporary permission to see a `String` I'm keeping elsewhere". But you're not keeping the `String` anywhere, you throw it away immediately. `&str` can't exist by itself.

```rust
.map(|(duration, value)| (duration.to_string(), *value))

```

---

<div class="post-metadata">

**Author:** ![mhanusek](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mhanusek/32/14096_2.png) [@mhanusek](https://users.rust-lang.org/u/mhanusek)\
**Post date:** [March 24, 2021, 3:34pm UTC](https://users.rust-lang.org/t/how-to-convert-a-vec-duration-to-vec-str-u64/57378/3 "2021-03-24T15:34:26Z")

</div>

My goal is to pass data to tui's function :

> **[BarChart in tui::widgets - Rust](https://docs.rs/tui/0.1.3/tui/widgets/struct.BarChart.html#method.data)**
>
> Display multiple bars in a single widgets

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [March 24, 2021, 3:36pm UTC](https://users.rust-lang.org/t/how-to-convert-a-vec-duration-to-vec-str-u64/57378/4 "2021-03-24T15:36:52Z")

</div>

So you will need a two-step process. First make pairs of `(String, u64)` and then, while a vec of them is holding them and keeping them alive, make another vec that temporarily borrows them.

```rust
let owned_strings: Vec<(String, u64)> = …;
let borrowed_strings: Vec<(&str, u64)> = owned_strings.iter().map(|(s, v)| (s.as_str(), *v)).collect();

```

This is because Rust needs to free every string you created, but without a garbage collector Rust has no way of tracking how many strings exist and which are still needed. Holding them in a `Vec` makes it possible to track them and free them when they're not needed any more.

---

<div class="post-metadata">

**Author:** ![mhanusek](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mhanusek/32/14096_2.png) [@mhanusek](https://users.rust-lang.org/u/mhanusek)\
**Post date:** [March 24, 2021, 3:48pm UTC](https://users.rust-lang.org/t/how-to-convert-a-vec-duration-to-vec-str-u64/57378/5 "2021-03-24T15:48:51Z")

</div>

```rust
let owned_strings: Vec<(String, u64)> = m.freq_map().iter().map(|(d,c)|{(d.as_millis().to_string(),*c)}).collect();
let _v: Vec<(&str, u64)> = owned_strings.iter().map(|(t,c)|{(t.as_str(), *c)}).collect();

```

@kornel ?

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [March 24, 2021, 4:30pm UTC](https://users.rust-lang.org/t/how-to-convert-a-vec-duration-to-vec-str-u64/57378/6 "2021-03-24T16:30:25Z")

</div>

Yes, that should work.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [June 22, 2021, 4:31pm UTC](https://users.rust-lang.org/t/how-to-convert-a-vec-duration-to-vec-str-u64/57378/7 "2021-06-22T16:31:04Z")

</div>

Closed on June 22, 2021, 4:31pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
