Hi guys,
The most complicated topic of all beginner rusters. Strings. Can someone give some hint how can I return a static str? I thought I understood that topic but now I am struggling again
The following code gives me an error:
use std::str;
pub fn get_str() -> &'static str {
let result: [u8; 3] = [97u8, 98u8, 99u8];
return &str::from_utf8(&result).unwrap()[..]; // error[E0515]: cannot return value referencing local variable `result`
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_str() {
assert_eq!(get_str(), "abc");
}
}
By the way I am not sure why I need &'static str
. Can I just return str
instead? And why everything is so complicated with those &str
? IMHO why cannot we just have String
and str
like everything else has? String
is heap, str[size]
is stack???