Create a function that gives a personalized greeting. This function takes two parameters: name and owner.
Use conditionals to return the proper message:
| case | return |
|---|---|
| name equals owner | ‘Hello boss’ |
| otherwise | ‘Hello guest’ |
Code — Written
A problem I am facing is return from a function, giving a string
So let’s research about it !
The return keyword can be used to return a value inside a function’s body. When this keyword isn’t used, the last expression is implicitly considered to be the return value. If a function returns a value, its return type is specified in the signature using -> after the parentheses ().
Statement vs. Expression
- A statement does not return a value, so it cannot be used to assign a value to another variable. For example, in some languages, we may be allowed to write
x = y = 5; but in Rust,let x = (let y = 5)is an invalid statement because thelet y = 5statement does not return a value that can be assigned tox. - Expressions can be a part of a statement.
- When a semi-colon is written at the end of an expression, it turns into a statement and does not return a value.
{}blocks and function calls are expressions.
As my code is;
fn greet(name: &str, owner: &str) -> String {
if name == owner {
"Hello boss"
}
else {"Hello guest"}
}Code — Using Match
fn greet(name: &str, owner: &str) -> String {
match name == owner{
true => "Hello boss".to_string(),
false => "Hello guest".to_string(),
}
}The return value is not in String, it is in str ?
- 🔼 Learn the Difference between Str and String#task