0

So I have this simple program that uppercases the string passed,

fn main() {
    let string = String::from("test here");
    let result = test(&string);
    println!("{}", &result);
}

fn test(param: &String) -> String {
    let upper = param.to_uppercase();
    return upper;
}

Now what I don't understand is if I should use param or &param inside test function body. string is already passed as a reference so should I use just param.to_uppercase() or does it still make sense to reference the param itself so something like &param.to_uppercase()?

Axel
  • 4,365
  • 11
  • 63
  • 122
  • 4
    A couple of things: 1. `return upper;` at the end of a function should simply be `upper`. 2. [Why is it discouraged to accept a reference to a String (&String), Vec (&Vec), or Box (&Box) as a function argument?](https://stackoverflow.com/q/40006219) 3. `&param.to_uppercase()` is parsed as `&(param.to_uppercase())`. – L. F. Jun 20 '21 at 13:10
  • 2
    To extend on what @L.F. said: your test function should be `fn test(param: &str) -> String { param.to_uppercase() }` – Denys Séguret Jun 20 '21 at 13:34
  • The problem to answer what seems to be the core of the question is that it's not clear why you'd want to take the reference of a reference, or what you imagine would be `&param.to_uppercase()`. – Denys Séguret Jun 20 '21 at 13:55

1 Answers1

2

Presuming you're asking about (&param).to_uppercase()

You should just use param, &param is of type &&String and that would have no value, (but no cost either if you did this by mistake).

weston
  • 54,145
  • 21
  • 145
  • 203