You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: user-guide/type-mapping/characters.qmd
+79Lines changed: 79 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,85 @@ title: "Character Strings"
7
7
library(rextendr)
8
8
```
9
9
10
+
The standard type for a UTF-8 encoded string type is `String`. An example of
11
+
instantiating such a type
12
+
13
+
```{extendr, echo=TRUE}
14
+
let mut rust_string = String::new();
15
+
rust_string.push_str("Hello world!");
16
+
rust_string
17
+
```
18
+
19
+
A direct translation of this to R is
20
+
```{r}
21
+
r_string <- "Hello world!"
22
+
r_string
23
+
```
24
+
25
+
Indeed, these are the same as they contain the same utf-8 bytes
26
+
27
+
```{r}
28
+
charToRaw(r_string)
29
+
```
30
+
31
+
```{extendr}
32
+
let bytes = String::from("Hello world!");
33
+
let bytes = bytes.as_bytes().to_owned();
34
+
bytes
35
+
```
36
+
37
+
A `character`-vector in R could be compared to a `Vec<String>` in Rust. However, there is an important distinction, that we'll illustrate with an example.
38
+
39
+
```{extendr}
40
+
let states = ["Idaho", "Texas", "Maine"]; // 5 letter states in USA
41
+
let b_states = states.into_iter().map(|x| x.as_bytes()).flatten().collect::<Vec<_>>();
42
+
b_states
43
+
```
44
+
45
+
And in R
46
+
47
+
```{r}
48
+
# charToRaw(c("Idaho", "Texas", "Maine")) // only uses first argument
0 commit comments