Rust ownership error when pushing String into Vec<String>

Hello everyone,

I’m currently learning Rust and I’m working on a small project where I consume data from the GitHub API and generate a PDF report.

I’m getting an error in this part of the code and I can’t figure out what I’m doing wrong:

use reqwest::blocking::Client;

use crate::{
    core::api::get_data,
    infra::pdf::generate_pdf,
    model::github_events::Events,
};

fn response_vec_string(client: &Client, name_user: String) {
    let vec_receive = get_data(client, name_user).unwrap();

    let response = search_name_respository(&vec_receive);

    match generate_pdf(&response, &vec_receive) {
        Ok(message) => {
            println!("{message:?}");
        }
        Err(err) => panic!("{:?}", err),
    }
}

fn search_name_respository(
    vector_repository: &Vec<Events>,
) -> Vec<String> {

    let mut vec_response: Vec<String> = Vec::new();

    for value in vector_repository {

        if verification_not_exits(
            &value.repo.name,
            &vec_response,
        ) != true {

            vec_response.push(
                &value.repo.name.to_string(),
            );
        }
    }

    vec_response
}

fn verification_not_exits(
    name_repository: &String,
    vec_name_repository: &Vec<String>,
) -> bool {

    let mut auxiliary: bool = false;

    for value in vec_name_repository {

        if value == name_repository {
            auxiliary = true;
        }
    }

    auxiliary
}

The error appears in this line:

vec_response.push(&value.repo.name.to_string());

Could someone explain why this happens and what would be the correct way to handle it in Rust?

```bash id="a9q7dr"
Compiling github-user-activity v0.1.0
(/home/karurosu/Documents/programming/rust/projects/cli/github-user-activity)

error[E0308]: mismatched types
  --> src/infra/names_respositorys.rs:20:31
   |
20 |             vec_response.push(&value.repo.name.to_string());
   |                          ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |                          |
   |                          expected `String`, found `&String`
   |                          arguments to this method are incorrect
   |
note: method defined here
  --> /home/karurosu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/
      lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:993:12
   |
993 |     pub fn push(&mut self, value: T) {
   |            ^^^^

help: consider removing the borrow

20 -             vec_response.push(&value.repo.name.to_string());
20 +             vec_response.push(value.repo.name.to_string());

For more information about this error, try `rustc --explain E0308`.

error: could not compile `github-user-activity` (lib)
due to 1 previous error
```

Thanks in advance.

Please post the error message so that we can understand how to help you.

It’s a Vec<String>, not a Vec<&String> or Vec<&str>. You should remove the & and just push value.repo.name.to_string().

(Though, there are likely also broader “you should do X” issues, this is just the minimal immediate solution. Also, I’d prefer .to_owned() or .clone() over .to_string(); the first two methods are more specific.)

I would replace your last two functions with something like the following: Rust Playground

Rust’s standard library types have a lot of utility methods (compared to, say, C++); be sure to use them.

Also, even though taking an owned Vec<T> or a mutable &mut Vec<T> as a function argument is useful, taking a &Vec<T> is almost always worse than taking a &[T]; the only thing you can do with an immutable vector but not an immutable slice is read the .capacity() of the vector (and other capacity-related stuff).

Thank you very much, you comment was useful.

Thank you very much; it really helped me improve my code and learn a new method from the standard library.

Note: the compiler was actually giving you the solution here!