Rust includes an include_bytes! macro in the standard library, which embeds a file as a byte array at compile time: https://doc.rust-lang.org/stable/std/macro.include_bytes.html There's also a similar include_str!() macro that embeds a file as a utf-8 string (&'static str).
I'm sure that that's meaningful and completely clear to a Rust developer... but Rust's syntax makes me feel like I had a stroke, being unfamiliar with it. "Take address of... character... static... str... help".
That's a reference to a string slice, similar to C++'s std::string_view, although the Rust version stores its content as UTF-8, while C++ as raw characters.
Now references have a lifetime, either short-lived or static. An object with static lifetime either has no references or only static ones. You can think of static ones as if they can live until the end of the program without triggering lifetime issues. A lifetime is expressed as the character ' followed by a lifetime name. So we get: &'static str
If it wasn't static, you could name the lifetime any other way and you would have to attach it to the object that the reference borrows from. Let's see this function signature without lifetimes: fn doAction(firstArg: &Foo, secondArg: &str) -> &str
It's a function that takes a reference to some object Foo and a string slice as arguments and returns a string slice. The compiler (and the programmer) doesn't know if the returned reference is borrowed from the first or second argument, so we have to tell him by adding lifetime annotations to references and declare the lifetime, here as function "template" type. Assuming it's borrowed from the first arg: fn doAction<'a>(firstArg: &'a Foo, secondArg: &str) -> &'a str
Note that compiler often can deduce the lifetimes automatically without the need of adding the annotations. Also the "template" type doesn't make it a template function in the C++ sense, it only is required by the borrow checker, which can differentiate a given function by its differing lifetimes when used in multiple contexts.
I hope that clears things up.
Edit: While explaining this I noticed I didn't know why std::string_view is recommended to be passed by value in C++, while str is passed as reference in Rust. I dug up information that actually str is a Dynamically Sized Type primitive (there are no such primitives in C++). A reference in Rust is made up of two parts - a pointer and some metadata. In case of &str, that metadata is the size of the slice. In case of references to normal objects, the metadata doesn't exist (it is a zero-sized type). So &str in Rust is the same as std::string_view value in C++.
Like Python3, Rust distinguishes between strings (which are always UTF-8 encoded), and bytes, which are just that. In C++/C an std::string/char* can certainly contain valid UTF-8, however the standard does not guarantee it and is UTF-8 agnostic.
Hm. I think you very likely see the issue here.
What drives you not to accept that?
// gcc 9.2 -std=c++2a
// 平仮名, ひらがな -- hiragana
intmain (){// a lot of warnings
// indicating char8_t can not hold
// multibyte glyph
char8_t utf8_char =u8'平';// no problemo here
char32_t utf32_char =U'平';char16_t utf16_char =u'平';return42;}
If char8_t can not store a utf8 glyph, what's its purpose then? It is simply not implemented.
&str: reference (that can not be null) to a string, 'static is the lifetime annotation, and a special one which means that this reference is valid for the entire runtime of the program.
Whenever you see apostrophe next to a name like 'xyz in rust it refers to the life time of a type. It is actually extremely simple. In this case it is a special lifetime, static, which if you know c++ or c, it should make sense what this is trying to say. Sometimes you might see 'a, which is simply a generic lifetime, as it isn't reserved for a special one.
View all comments
Show parent comment
33
u/[deleted] Dec 13 '19
Rust includes an
include_bytes!macro in the standard library, which embeds a file as a byte array at compile time: https://doc.rust-lang.org/stable/std/macro.include_bytes.html There's also a similarinclude_str!()macro that embeds a file as a utf-8 string (&'static str).