go1 works but go5 has a error which says we are returning reference to something owned by current function.
(123+1) is also owned by the function why can we return it?
why does go5 fail?
I don't think there is a limit on what types can be returned with static lifetimes from functions. However, values do not gain the static lifetime by being returned from a function just because it declares it's return type has a static lifetime. you can declare static variables with the static keyword using a similar syntax to the let keyword.
static foo: i32 = 42;
The value created by the expression (123+x) is not static and gets dropped as soon as it goes out of scope. String literals are 'static by default even without being declared with the static keyword. that's why go2 works. for go3, trim returns an &str and when that reference or slice is to an already static string literal it can also be a static reference. for go1, there might actually be some coercion going on. some programming languages will sometimes store some small numbers or integers in the static section of the program. python actually does this despite being bytecode interpreted.
static variables are a bit complicated, but if you declare your variables with the static keyword and ensure they are initialized with constant expressions that can be evaluated at compile time they should work fine. further reading:: Static items - The Rust Reference
What rvalues can be promoted? What I got from all three sources was that only expressions which you can put in a const can be promoted.
but const A:&str=" qde".trim(); causes a error
while this is fine
That's the point. The rule of thumb defined in the first link says:
// if this compiles
const X: &'static T = &CONST_EXPR;
// this should also compile
let x: &'static T = &CONST_EXPR;
// or equivalently, `x` can be returned from a function as a 'static reference
So you've already seen const A:&str=" qde".trim(); doesn't compile, thus it can't be returned as a 'static reference.
For " qde".trim(), it's not a const expression as the third link defines.
There's no promotion happening in the trim case. If you start out with a &'static str then .trim() can turn that into a string slice of the same lifetime, as it merely returns a new view into the same data.
Note that string literals like "qde" are already &'static str, if you borrow them again you get a &'something &'static str, which however the compiler can easily turn back into &'static str by automatically reborrowing.