- briansmith:
- Confusion about &String coercing to &str:
- Thought this was because of AsRef, but now thinks is probably because of Deref.
- &'static str is immutable, but String is mutable.
- Similar to C++; not in a good way.
- Confusion about &String coercing to &str:
- DanielKeep:
- capacity == 0 && len != 0 to indicate contents are static.
- rkruppe:
- Very common speed bump on the learning curve, but it goes much, much
deeper than string literals not being Strings:
- Often not solvable via sprinkling .to_string()
- Write &str but meant String
- Want to modify &str
- !!! Which to store in a struct: &str or String
- Proposed implict conversion delays but doesn't solve the "BTW, two (and
more) string types"
- Adding one more thing to explain at that point isn't helping
- Brings up that explaining
"foo"
is sometimes String but othertimes not will be difficult- Current: String literals are &'static str
- Future: String literals are &'static str except sometimes they are String but this only works for literals not for other &str value you want to convert.
- Conversation:
- leodasvacas: Can be explained via 'string literals just work' and leave rest of explanation as-is
- rkruppe: Coercions can't always "Just work"; see deref coercions
- Split between &str and String is hard to 'hide'
- Perf concerns:
- String turns into more compact Cow<'static, str>
- Very common speed bump on the learning curve, but it goes much, much
deeper than string literals not being Strings:
- withoutboats:
- Keynote @ RustConf brought up closure auto-impl of Fn traits
- Veedrac:
- &str/String conversions need to be general or "last thing Rust needs is another rule that doesn't generalize"
- nrc:
- "From Literal" (via magic, probably):
- SimpleString crate, for new users, which allocates by-default.
- Should be transparent (hence the magic)
- Support for custom literals would also be useful for BigNum types and
other numerical libraries.
- (Maybe) generalized to number literals
- SimpleString crate, for new users, which allocates by-default.
- "From Literal" (via magic, probably):
Last active
September 22, 2016 02:20
-
-
Save Mark-Simulacrum/40618d40316dec78c15af4231c6d1719 to your computer and use it in GitHub Desktop.
SimpleString and the &'static str vs. String problem for beginners
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
// Preserve these cases: | |
let s = "foo"; | |
own(s.to_string()); // 1: Should not auto-convert s -> String | |
// Should work, without into() for ergonomics: | |
let s: SimpleString = "foo"; | |
let s: String = "foo"; | |
} | |
use std::convert::From; | |
use std::ops::Deref; | |
fn own(s: String) { std::mem::drop(s); } | |
struct SimpleString(String); | |
impl<'a> From<&'a str> for SimpleString { | |
fn from(from: &'a str) -> Self { | |
SimpleString(from.into()) | |
} | |
} | |
impl From<String> for SimpleString { | |
fn from(from: String) -> Self { | |
SimpleString(from) | |
} | |
} | |
impl Deref for SimpleString { | |
type Target = str; | |
fn deref(&self) -> &str { | |
&self.0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment