Borrowed value does not live long enough when iterating over a generic value with a lifetime on the function...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















fn func<'a, T>(arg: Vec<Box<T>>)
where
String: From<&'a T>,
T: 'a,
{
let s: Vec<String> = arg.iter().map(|s| String::from(s)).collect();
do_something_else(arg);
}

fn do_something_else<T>(arg: Vec<Box<T>>) {}


The compiler complains that arg does not live long enough. Why though?



error[E0597]: `arg` does not live long enough
--> src/lib.rs:6:26
|
6 | let s: Vec<String> = arg.iter().map(|s| String::from(s)).collect();
| ^^^ borrowed value does not live long enough
7 | do_something_else(arg);
8 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 1:9...
--> src/lib.rs:1:9
|
1 | fn func<'a, T>(arg: Vec<Box<T>>)
| ^^









share|improve this question

























  • So you want to use arg after collecting the members into s? Else I would say use into_iter and it's all done.

    – hellow
    Nov 26 '18 at 16:58


















3















fn func<'a, T>(arg: Vec<Box<T>>)
where
String: From<&'a T>,
T: 'a,
{
let s: Vec<String> = arg.iter().map(|s| String::from(s)).collect();
do_something_else(arg);
}

fn do_something_else<T>(arg: Vec<Box<T>>) {}


The compiler complains that arg does not live long enough. Why though?



error[E0597]: `arg` does not live long enough
--> src/lib.rs:6:26
|
6 | let s: Vec<String> = arg.iter().map(|s| String::from(s)).collect();
| ^^^ borrowed value does not live long enough
7 | do_something_else(arg);
8 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 1:9...
--> src/lib.rs:1:9
|
1 | fn func<'a, T>(arg: Vec<Box<T>>)
| ^^









share|improve this question

























  • So you want to use arg after collecting the members into s? Else I would say use into_iter and it's all done.

    – hellow
    Nov 26 '18 at 16:58














3












3








3








fn func<'a, T>(arg: Vec<Box<T>>)
where
String: From<&'a T>,
T: 'a,
{
let s: Vec<String> = arg.iter().map(|s| String::from(s)).collect();
do_something_else(arg);
}

fn do_something_else<T>(arg: Vec<Box<T>>) {}


The compiler complains that arg does not live long enough. Why though?



error[E0597]: `arg` does not live long enough
--> src/lib.rs:6:26
|
6 | let s: Vec<String> = arg.iter().map(|s| String::from(s)).collect();
| ^^^ borrowed value does not live long enough
7 | do_something_else(arg);
8 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 1:9...
--> src/lib.rs:1:9
|
1 | fn func<'a, T>(arg: Vec<Box<T>>)
| ^^









share|improve this question
















fn func<'a, T>(arg: Vec<Box<T>>)
where
String: From<&'a T>,
T: 'a,
{
let s: Vec<String> = arg.iter().map(|s| String::from(s)).collect();
do_something_else(arg);
}

fn do_something_else<T>(arg: Vec<Box<T>>) {}


The compiler complains that arg does not live long enough. Why though?



error[E0597]: `arg` does not live long enough
--> src/lib.rs:6:26
|
6 | let s: Vec<String> = arg.iter().map(|s| String::from(s)).collect();
| ^^^ borrowed value does not live long enough
7 | do_something_else(arg);
8 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 1:9...
--> src/lib.rs:1:9
|
1 | fn func<'a, T>(arg: Vec<Box<T>>)
| ^^






rust lifetime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 17:13









Shepmaster

161k16330474




161k16330474










asked Nov 26 '18 at 16:45









NirmanNirman

534




534













  • So you want to use arg after collecting the members into s? Else I would say use into_iter and it's all done.

    – hellow
    Nov 26 '18 at 16:58



















  • So you want to use arg after collecting the members into s? Else I would say use into_iter and it's all done.

    – hellow
    Nov 26 '18 at 16:58

















So you want to use arg after collecting the members into s? Else I would say use into_iter and it's all done.

– hellow
Nov 26 '18 at 16:58





So you want to use arg after collecting the members into s? Else I would say use into_iter and it's all done.

– hellow
Nov 26 '18 at 16:58












1 Answer
1






active

oldest

votes


















6














The constraint String: From<&'a T>, with emphasis on the function's lifetime parameter 'a, would allow you to convert a reference to T to a String. However, the reference to the elements obtained from the iterator is more restrictive than 'a (hence, they do not live long enough).



Since the conversion is supposed to work fine for references of any lifetime, you may replace the constraint with a higher ranked trait bound (HRTB):



fn func<T>(arg: Vec<Box<T>>)
where
for<'a> String: From<&'a T>,
{
let s: Vec<String> = arg.iter().map(|s| String::from(s)).collect();
do_something_else(arg);
}


The use of From here to obtain an owned string is also not something I've seen in the wild. Perhaps you would be interested in the Display trait, so that you can call to_string():



fn func<T>(arg: Vec<Box<T>>)
where
T: Display,
{
let _: Vec<_> = arg.iter().map(|s| s.to_string()).collect();
// ...
}


See also:




  • How do I write the lifetimes for references in a type constraint when one of them is a local reference?

  • How does for<> syntax differ from a regular lifetime bound?






share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53485574%2fborrowed-value-does-not-live-long-enough-when-iterating-over-a-generic-value-wit%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    The constraint String: From<&'a T>, with emphasis on the function's lifetime parameter 'a, would allow you to convert a reference to T to a String. However, the reference to the elements obtained from the iterator is more restrictive than 'a (hence, they do not live long enough).



    Since the conversion is supposed to work fine for references of any lifetime, you may replace the constraint with a higher ranked trait bound (HRTB):



    fn func<T>(arg: Vec<Box<T>>)
    where
    for<'a> String: From<&'a T>,
    {
    let s: Vec<String> = arg.iter().map(|s| String::from(s)).collect();
    do_something_else(arg);
    }


    The use of From here to obtain an owned string is also not something I've seen in the wild. Perhaps you would be interested in the Display trait, so that you can call to_string():



    fn func<T>(arg: Vec<Box<T>>)
    where
    T: Display,
    {
    let _: Vec<_> = arg.iter().map(|s| s.to_string()).collect();
    // ...
    }


    See also:




    • How do I write the lifetimes for references in a type constraint when one of them is a local reference?

    • How does for<> syntax differ from a regular lifetime bound?






    share|improve this answer






























      6














      The constraint String: From<&'a T>, with emphasis on the function's lifetime parameter 'a, would allow you to convert a reference to T to a String. However, the reference to the elements obtained from the iterator is more restrictive than 'a (hence, they do not live long enough).



      Since the conversion is supposed to work fine for references of any lifetime, you may replace the constraint with a higher ranked trait bound (HRTB):



      fn func<T>(arg: Vec<Box<T>>)
      where
      for<'a> String: From<&'a T>,
      {
      let s: Vec<String> = arg.iter().map(|s| String::from(s)).collect();
      do_something_else(arg);
      }


      The use of From here to obtain an owned string is also not something I've seen in the wild. Perhaps you would be interested in the Display trait, so that you can call to_string():



      fn func<T>(arg: Vec<Box<T>>)
      where
      T: Display,
      {
      let _: Vec<_> = arg.iter().map(|s| s.to_string()).collect();
      // ...
      }


      See also:




      • How do I write the lifetimes for references in a type constraint when one of them is a local reference?

      • How does for<> syntax differ from a regular lifetime bound?






      share|improve this answer




























        6












        6








        6







        The constraint String: From<&'a T>, with emphasis on the function's lifetime parameter 'a, would allow you to convert a reference to T to a String. However, the reference to the elements obtained from the iterator is more restrictive than 'a (hence, they do not live long enough).



        Since the conversion is supposed to work fine for references of any lifetime, you may replace the constraint with a higher ranked trait bound (HRTB):



        fn func<T>(arg: Vec<Box<T>>)
        where
        for<'a> String: From<&'a T>,
        {
        let s: Vec<String> = arg.iter().map(|s| String::from(s)).collect();
        do_something_else(arg);
        }


        The use of From here to obtain an owned string is also not something I've seen in the wild. Perhaps you would be interested in the Display trait, so that you can call to_string():



        fn func<T>(arg: Vec<Box<T>>)
        where
        T: Display,
        {
        let _: Vec<_> = arg.iter().map(|s| s.to_string()).collect();
        // ...
        }


        See also:




        • How do I write the lifetimes for references in a type constraint when one of them is a local reference?

        • How does for<> syntax differ from a regular lifetime bound?






        share|improve this answer















        The constraint String: From<&'a T>, with emphasis on the function's lifetime parameter 'a, would allow you to convert a reference to T to a String. However, the reference to the elements obtained from the iterator is more restrictive than 'a (hence, they do not live long enough).



        Since the conversion is supposed to work fine for references of any lifetime, you may replace the constraint with a higher ranked trait bound (HRTB):



        fn func<T>(arg: Vec<Box<T>>)
        where
        for<'a> String: From<&'a T>,
        {
        let s: Vec<String> = arg.iter().map(|s| String::from(s)).collect();
        do_something_else(arg);
        }


        The use of From here to obtain an owned string is also not something I've seen in the wild. Perhaps you would be interested in the Display trait, so that you can call to_string():



        fn func<T>(arg: Vec<Box<T>>)
        where
        T: Display,
        {
        let _: Vec<_> = arg.iter().map(|s| s.to_string()).collect();
        // ...
        }


        See also:




        • How do I write the lifetimes for references in a type constraint when one of them is a local reference?

        • How does for<> syntax differ from a regular lifetime bound?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 27 '18 at 9:41

























        answered Nov 26 '18 at 17:04









        E_net4E_net4

        12.8k73872




        12.8k73872
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53485574%2fborrowed-value-does-not-live-long-enough-when-iterating-over-a-generic-value-wit%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen