Missing build warning "temporary_string_as_ptr" when String is used with "as_ptr" on one line #90449
Labels
A-lints
Area: Lints (warnings about flaws in source code) such as unused_mut.
C-feature-request
Category: A feature request, i.e: not implemented / a PR.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
T-lang
Relevant to the language team, which will review and decide on the PR/issue.
Playground
It's rather a suggestion than a bug.
Have this code:
Rust reports the warning
rustc(temporary_cstring_as_ptr)
during build. The detail isthis `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
which is expected 🆗 .If you run the code, a "random" character will be printed and that's of course unexpected. A solution is an assignment of the string into a standalone variable as explained here.
Have the same code, but this time it uses
String
instead ofCString
:I expected to see this happen:
CString
, because also in this case the pointer is dangling and if you run the code, a random character will be printed. A solution is the same as forCString
Instead, this happened:
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: