#Error borrowed value does not live long enough on function parameter

7 messages · Page 1 of 1 (latest)

green belfry
#

I've given the complete error on a screenshot, but basically not understanding why after cloning the parameter I'm still getting this message. There's a warning that cloned_md_string needs to be borrowed as static but if I'm cloning it how am I still borrowing it?

pub fn convert_md_to_html(&self, md_string: String) -> Vec<HtmlElement<AnyElement>> {
        let cloned_md_string = md_string.clone();
        let md_lines = cloned_md_string.split('\n').collect::<Vec<&str>>();
        let mut html_lines: Vec<HtmlElement<AnyElement>> = vec![];

        let mut ol_started = false;
        let mut current_found_ol: Vec<HtmlElement<AnyElement>> = vec![];

        let mut ul_started = false;
        let mut current_found_ul: Vec<HtmlElement<AnyElement>> = vec![];

        let mut code_started = false;
        let mut code_ended = false;
        let mut code_sections: Vec<HtmlElement<AnyElement>> = vec![];

        for md_line in md_lines.clone() {            
            let cloned_line = md_line.clone();
// more code here
undone widget
green belfry
#

If you look at the code I am cloning all strings coming off of the parameter md_string as cloned_md_string. I'm even cloning the string md_line as cloned_line

undone widget
#

you'll want to either:

  1. pass in md_string as a &str from the caller; it'll still give your returned vec a limited lifetime based on the original place it came from but it can get outside of the function
    or 2. convert each line to String and find out a way to get HtmlElement / AnyElement to take String instead of &str
#

the thing to note is that clone() doesn't necessarily put some data in its own allocation:
cloning String makes a fully independent deep copy
cloning a &str just makes another &str pointing to the same data
calling .to_owned() on a &str will copy the underlying data and give you a separate String that doesn't have lifetime constraints

undone widget
#

specifically, on line 81, if you pass cloned_line.to_owned() into child() it should take it, you could actually refactor out the clone calls too