sad
sad

Reputation: 137

"lifetime mismatch ...but data from is returned here" when returning a vector

main

use std::collections::HashMap;

use crate::math_parser::pconsts;

/// Math problems parser.
pub struct Parser {
    pub string: &'static str
}

impl Parser {
    /// Get given string.
    pub fn get_str(&self) -> &str {
        return self.string;
    }

    /// Get math problems separated by commas.
    pub fn get_problems(&self) -> Vec<&str> {
        return self.string.split(pconsts::parser_comma).collect();
    }

    /// Trim math problems.
    pub fn trim_problems(&self, problems: Vec<&str>) -> Vec<&str> {
        let trimed_problems: Vec<&str>;

        for problem in problems {
            trimed_problems.push(problem);
        }

        return trimed_problems;
    }

    // /// Parse math problems.
    // pub fn parse(&self) -> Vec<HashMap<&str, &str>> {
        
    // }
}

when compiling getting this error.

error[E0623]: lifetime mismatch
  --> src\math_parser\parser.rs:29:16
   |
22 |     pub fn trim_problems(&self, problems: Vec<&str>) -> Vec<&str> {
   |                                               ----      ---------
   |                                               |
   |                                               this parameter and the return type are declared with different lifetimes...
...
29 |         return trimed_problems;
   |                ^^^^^^^^^^^^^^^ ...but data from `problems` is returned here

How do I solve this?

Upvotes: 0

Views: 249

Answers (1)

sad
sad

Reputation: 137

    /// Trim math problems vector.
    pub fn trim_problems<'a>(&self, problems: Vec<&'a str>) -> Vec<&'a str> {
        let mut trimed_problems: Vec<&str> = vec![];

        for problem in problems {
            trimed_problems.push(problem.trim());
        }

        return trimed_problems;
    }

Solved.

Upvotes: 2

Related Questions