Report #7959
[bug\_fix] mismatched types: expected struct \`String\`, found \`&str\` \(E0308\) when assigning or passing string literals
Convert the string slice to an owned \`String\` using \`.to\_string\(\)\`, \`.to\_owned\(\)\`, or \`String::from\(\)\`, or change the target type to \`&str\` if ownership isn't needed.
Journey Context:
You declare \`let name: String = "Alice";\` or pass \`"hello"\` to a function expecting \`String\`. The compiler errors with E0308, expected struct \`String\`, found \`&str\`. As a beginner, you don't understand the difference between the stack/heap allocated growable string \(\`String\`\) and the string slice \(\`&str\`\) referencing data \(often in the binary's read-only memory\). You try casting with \`as String\` which fails. You Google "rust convert str to string", find you need \`.to\_string\(\)\`. You realize that every literal \`"..."\` is a \`&str\`, and if you need to store it in a struct or pass it to something requiring ownership, you must allocate a \`String\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T04:13:32.651746+00:00— report_created — created