rust-by-practice/solutions/compound-types/string.md

198 lines
3.2 KiB
Markdown
Raw Normal View History

2022-05-01 00:08:50 -05:00
1.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let s: &str = "hello, world";
}
```
2022-05-01 00:08:50 -05:00
2.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
2022-07-04 06:51:02 -05:00
let s: Box<str> = "hello, world".into();
2022-03-02 06:57:13 -06:00
greetings(&s)
}
fn greetings(s: &str) {
println!("{}",s)
}
```
2022-05-01 00:08:50 -05:00
2022-03-15 19:22:24 -06:00
```rust
fn main() {
let s: Box<&str> = "hello, world".into();
greetings(*s)
}
fn greetings(s: &str) {
println!("{}", s);
}
```
2022-03-02 06:57:13 -06:00
2022-05-01 00:08:50 -05:00
3.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let mut s = String::new();
s.push_str("hello, world");
s.push('!');
assert_eq!(s, "hello, world!");
}
```
2022-05-01 00:08:50 -05:00
4.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
2022-07-04 06:51:02 -05:00
let mut s = String::from("hello");
2022-03-02 06:57:13 -06:00
s.push(',');
s.push_str(" world");
s += "!";
println!("{}", s)
}
```
2022-05-01 00:08:50 -05:00
5.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let s = String::from("I like dogs");
// Allocate new memory and store the modified string there
let s1 = s.replace("dogs", "cats");
assert_eq!(s1, "I like cats")
}
```
2022-05-01 00:08:50 -05:00
6.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let s1 = String::from("hello,");
let s2 = String::from("world!");
let s3 = s1.clone() + &s2;
assert_eq!(s3,"hello,world!");
println!("{}",s1);
}
```
2022-05-01 00:08:50 -05:00
7.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
2022-07-04 06:51:02 -05:00
let s = "hello, world".to_string();
2022-03-02 06:57:13 -06:00
greetings(s)
}
fn greetings(s: String) {
println!("{}",s)
}
```
```rust
fn main() {
let s = String::from("hello, world");
greetings(s)
}
fn greetings(s: String) {
println!("{}",s)
}
```
2022-05-01 00:08:50 -05:00
8.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
2022-05-01 00:08:50 -05:00
let s = "hello, world".to_string();
2022-03-02 06:57:13 -06:00
let s1: &str = &s;
}
```
```rust
fn main() {
2022-07-04 06:51:02 -05:00
let s = "hello, world";
2022-03-02 06:57:13 -06:00
let s1: &str = s;
}
```
```rust
fn main() {
2022-05-01 00:08:50 -05:00
let s = "hello, world".to_string();
2022-03-02 06:57:13 -06:00
let s1: String = s;
}
```
2022-05-01 00:08:50 -05:00
9.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
// You can use escapes to write bytes by their hexadecimal values
// fill the blank below to show "I'm writing Rust"
let byte_escape = "I'm writing Ru\x73\x74!";
println!("What are you doing\x3F (\\x3F means ?) {}", byte_escape);
// ...or Unicode code points.
let unicode_codepoint = "\u{211D}";
let character_name = "\"DOUBLE-STRUCK CAPITAL R\"";
println!("Unicode character {} (U+211D) is called {}",
unicode_codepoint, character_name );
let long_string = "String literals
can span multiple lines.
The linebreak and indentation here \
can be escaped too!";
println!("{}", long_string);
}
```
2022-05-01 00:08:50 -05:00
10.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let raw_str = "Escapes don't work here: \x3F \u{211D}";
2022-11-21 07:32:34 -06:00
// modify above line to make it work
2022-03-02 06:57:13 -06:00
assert_eq!(raw_str, "Escapes don't work here: ? ");
// If you need quotes in a raw string, add a pair of #s
let quotes = r#"And then I said: "There is no escape!""#;
println!("{}", quotes);
// If you need "# in your string, just use more #s in the delimiter.
// You can use up to 65535 #s.
let delimiter = r###"A string with "# in it. And even "##!"###;
println!("{}", delimiter);
2022-11-21 07:32:34 -06:00
// Fill the blank
2022-03-02 06:57:13 -06:00
let long_delimiter = r###"Hello, "##""###;
assert_eq!(long_delimiter, "Hello, \"##\"")
}
```
2022-05-01 00:08:50 -05:00
11.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let s1 = String::from("hi,中国");
let h = &s1[0..1];
assert_eq!(h, "h");
let h1 = &s1[3..6];
assert_eq!(h1, "中");
}
```
2022-05-01 00:08:50 -05:00
12.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
for c in "你好,世界".chars() {
println!("{}", c)
}
}
2022-03-15 19:22:24 -06:00
```