Take a string slice and returns a reference to its first character
Hey all,
I am learning Rust, and was not sure how to approach the following:
Write a function `first_char` that takes a string slice and returns a reference to its first character.`
My attempt:
fn first_char(s: &str) -> Option<&char> {
s.chars().next().as_ref()
}
However, Rust complains that it "cannot return value referencing temporary value
returns a value referencing data owned by the current function"
Is there any way to solve the above? Any pointers would be appreciated