Params in Rust programming language

Hello everyone, I recently started learning rust.I am implementing rust concepts in basic programs.I struck in the declaration of function when the return type is vector with possiblity of two data types String and u32.Somewhere i came across this params is useful for this kind.But I am not sure of this.So,i request for the help in understanding this to explore myself.

enums

Hello,Could you please say me how enums are related to this with an example if possible.

enum MyReturnValue {
  AnInteger(u32),
  AString(String)
}

fn return_something(something: bool) -> MyReturnValue {
  if something {
    MyReturnValue::AnInteger(42)
  } else {
    MyReturnValue::AString("Hello.".to_string())
  }
}

fn caller() {
  match return_something(true) {
    MyReturnValue::AnInteger(val) => { /* handle int */ }
    MyReturnValue::AString(val) => { /* handle string */ }
  }
}

Thanku for your suggestion.I will try this out.

Hello, How to implement sort_by_key function for vector of integers as strings.
For example, v=["5","3","1","8"]
output must be: ["1","3","5","8"]

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.