Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Determine string length at compile time #2039

Closed
jD91mZM2 opened this issue Jun 23, 2017 · 4 comments
Closed

Determine string length at compile time #2039

jD91mZM2 opened this issue Jun 23, 2017 · 4 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@jD91mZM2
Copy link

So, imagine having a len! macro. Literally just that.

Why is this useful?

Well, there aren't a lot of reasons...
Use-case #1:

let world = "World";
let helloworld = String::with_capacity(len!("Hello "), world.len());
println!("{}", helloworld);

Use-case #2:

let string = "prefix!!yay";
if string.starts_with("prefix!!") {
    let string = &string[len!("prefix!!")..];
    assert_eq!(string, "yay");
}
@eddyb
Copy link
Member

eddyb commented Jun 23, 2017

Both of those examples could use .len().

@jD91mZM2
Copy link
Author

jD91mZM2 commented Jun 23, 2017

Except "prefix!!".len() isn't retrieved at compile time.

@eddyb
Copy link
Member

eddyb commented Jun 23, 2017

@legolord208 What difference does it make? It still optimizes to a constant, always.

@jD91mZM2
Copy link
Author

Oh 🤔.... Ooops, didn't realize

@Centril Centril added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Feb 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

3 participants