In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Rust string literal case analysis", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Rust string literal case analysis" bar!
Preface
There are two kinds of strings in Rust, String and & str, in which String can be dynamically assigned and modified, the internal implementation can be understood as Vec, and & str is a slice of type & [U8]. Both strings can hold only legitimate UTF-8 characters.
For UTF-8 characters that are not recognizable to the naked eye, consider using the following types:
Dedicated Path and PathBuf classes are available for the file path.
Use Vec and & [U8]
Use OSString and & OSStr to interact with the operating system
Use CString and & CStr to interact with C libraries
The second method above is the common way of dealing with non-UTF-8 byte streams, that is, using Vec and & [U8], where we can also use literals to deal with these two types of data, which we call byte string literals (byte string literals), of type & [U8].
String literals (String literals)
Let's first take a look at the literal value of the string.
Like other languages, a string is enclosed in double quotes, but one of the features of Rust is that the string can span lines, that is, a carriage return in the middle will not cause a compilation or run error, and it will be output with a newline character in it.
Similarly, escape is supported in string literals, for example, if you want to use double quotes in it, the escape will also escape the newline character, such as the following, using\ before the newline character, then the escape character, the newline character, and all spaces at the beginning of the next line will be ignored:
Let a = "foobar"; let b = "foo\ bar"; assert_eq!
String literals support the escape of Unicode in addition to the common\ escape of bytes (characters):
\ xHH: + 2-bit hexadecimal 7-bit width bytecode, which is equivalent to the equivalent ASCII character.
\ u {xxxx}: 24-bit hexadecimal that represents the equivalent Unicode character.
\ n /\ r /\ t stands for Ubun000A (LF), Ubun000D (CR) and Ubun0009 (HT)
Used to escape\ itself.
\ 0 means Unicode Utility 0000 (NUL)
The literal value of the string of type Raw indicates that it is escaped, that is, what the literal value is written about, the value of the string. This type of literal value is defined with r and several # at the beginning, with an equal number of # at the end.
As follows:
"foo"; r "foo"; / / foo "\" foo\ "; r #"foo"#; / /" foo "foo #\" # bar "; ritual #" foo # "# bar" # #; / / foo # "# bar"\ x52 ";" R "; r" R "; / / R"\ x52 "; r"\ x52 " / /\ x52
What if there are double quotation marks in the string? Because escaping cannot be used in raw string,\ "is definitely not possible. Rust actually supports the use of r # to specify string boundaries. This # is another way to escape. For example, if there are 4 # in a string, the string can be surrounded by r #" abc####def "#, that is, more than the # in it.
Byte string literals
Byte string literals use b "..." And the derivative syntax definition, whose type is & [U8], which is completely different from & str, so some methods that can be used on & str cannot be used on & [U8].
For example:
/ / & [U8; 5]: [11911114108100]! let world = b "world"; println! ("Hello, {}!", world)
Compilation will report an error because & [U8] does not implement std::fmt::Display:
29 | println! ("Hello, {}!", world)
| | ^ `[ u8; 5] `cannot be formatted with the default formatter |
| |
= help: the trait `std::fmt:: Display` is not implemented for `[u8; 5]`
= note: in format strings you may be able to use `{:?}` (or {: #?} for pretty-print) instead
= note: this error originates in the macro `$crate::format_args_ nl` (in Nightly builds, run with-Z macro-backtrace for more info)
Byte string literals also support escape, but it should be noted that it only supports byte escape, not Unicode escape.
/ / support character escape, output: Hello, rustlet escaped = b "\ x52\ x75\ x73\ x74 as bytes"; / / does not support Unicode escape, compilation error: / / = help: unicode escape sequences cannot be used as a byte or in a byte stringlet escaped = b "\ u {211D} is not allowed"; / / Raw byte strings work just like raw stringslet raw_bytestring = br "\ u {211D} is not escaped here"; println! ("{:?}", raw_bytestring) / / Converting a byte array to `str`can failif let Ok (my_str) = str::from_utf8 (raw_bytestring) {println! ("And the same as text:'{}'", my_str);}
Byte strings also support raw definitions, similar to standard string types, using the r prefix to define raw byte string literal variables.
For example, in the following example, a normal byte string needs to be escaped, and a raw byte string does not need to be escaped using\.
B "foo"; br "foo"; / / foob "\" foo\ "; br#"foo"#; / /" foo "b" foo #\ "# bar"; br## "foo #" # bar "# #; / / foo #" # barb "\ x52"; b "R"; br "R"; / / Rb "\ x52"; br "\ x52" / /\ x52 Thank you for your reading, the above is the content of "Rust string literal example Analysis". After the study of this article, I believe you have a deeper understanding of the problem of Rust string literal example analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.