add solutions for string.md
This commit is contained in:
parent
2d7e251e8b
commit
0f4685dcf0
|
@ -0,0 +1,174 @@
|
|||
1.
|
||||
```rust
|
||||
fn main() {
|
||||
let s: &str = "hello, world";
|
||||
}
|
||||
```
|
||||
|
||||
2.
|
||||
```rust
|
||||
fn main() {
|
||||
let s: Box<str> = "hello, world".into();
|
||||
greetings(&s)
|
||||
}
|
||||
|
||||
fn greetings(s: &str) {
|
||||
println!("{}",s)
|
||||
}
|
||||
```
|
||||
|
||||
3.
|
||||
```rust
|
||||
fn main() {
|
||||
let mut s = String::new();
|
||||
s.push_str("hello, world");
|
||||
s.push('!');
|
||||
|
||||
assert_eq!(s, "hello, world!");
|
||||
}
|
||||
```
|
||||
|
||||
4.
|
||||
```rust
|
||||
fn main() {
|
||||
let mut s = String::from("hello");
|
||||
s.push(',');
|
||||
s.push_str(" world");
|
||||
s += "!";
|
||||
|
||||
println!("{}", s)
|
||||
}
|
||||
```
|
||||
|
||||
5.
|
||||
```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")
|
||||
}
|
||||
```
|
||||
|
||||
6.
|
||||
```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);
|
||||
}
|
||||
```
|
||||
|
||||
7.
|
||||
```rust
|
||||
fn main() {
|
||||
let s = "hello, world".to_string();
|
||||
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)
|
||||
}
|
||||
```
|
||||
|
||||
8.
|
||||
```rust
|
||||
fn main() {
|
||||
let s = "hello, world".to_string();
|
||||
let s1: &str = &s;
|
||||
}
|
||||
```
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let s = "hello, world";
|
||||
let s1: &str = s;
|
||||
}
|
||||
```
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let s = "hello, world".to_string();
|
||||
let s1: String = s;
|
||||
}
|
||||
```
|
||||
|
||||
9.
|
||||
```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);
|
||||
}
|
||||
```
|
||||
|
||||
10.
|
||||
```rust
|
||||
fn main() {
|
||||
let raw_str = "Escapes don't work here: \x3F \u{211D}";
|
||||
// modify below line to make it work
|
||||
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);
|
||||
|
||||
// fill the blank
|
||||
let long_delimiter = r###"Hello, "##""###;
|
||||
assert_eq!(long_delimiter, "Hello, \"##\"")
|
||||
}
|
||||
```
|
||||
|
||||
11.
|
||||
```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, "中");
|
||||
}
|
||||
```
|
||||
|
||||
12.
|
||||
```rust
|
||||
fn main() {
|
||||
for c in "你好,世界".chars() {
|
||||
println!("{}", c)
|
||||
}
|
||||
}
|
||||
```
|
|
@ -0,0 +1,143 @@
|
|||
1.
|
||||
```rust
|
||||
fn main() {
|
||||
let x = 5;
|
||||
// fill the blank
|
||||
let p = &x;
|
||||
|
||||
println!("the memory address of x is {:p}", p); // one possible output: 0x16fa3ac84
|
||||
}
|
||||
```
|
||||
|
||||
2.
|
||||
```rust
|
||||
fn main() {
|
||||
let x = 5;
|
||||
let y = &x;
|
||||
|
||||
// modify this line only
|
||||
assert_eq!(5, *y);
|
||||
}
|
||||
```
|
||||
|
||||
3.
|
||||
```rust
|
||||
fn main() {
|
||||
let mut s = String::from("hello, ");
|
||||
|
||||
borrow_object(&s)
|
||||
}
|
||||
|
||||
fn borrow_object(s: &String) {}
|
||||
```
|
||||
|
||||
4.
|
||||
```rust
|
||||
fn main() {
|
||||
let mut s = String::from("hello, ");
|
||||
|
||||
push_str(&mut s)
|
||||
}
|
||||
|
||||
fn push_str(s: &mut String) {
|
||||
s.push_str("world")
|
||||
}
|
||||
```
|
||||
|
||||
5.
|
||||
```rust
|
||||
fn main() {
|
||||
let mut s = String::from("hello, ");
|
||||
|
||||
// fill the blank to make it work
|
||||
let p = &mut s;
|
||||
|
||||
p.push_str("world");
|
||||
}
|
||||
```
|
||||
|
||||
6.
|
||||
```rust
|
||||
fn main() {
|
||||
let c = '中';
|
||||
|
||||
let r1 = &c;
|
||||
// fill the blank,dont change other code
|
||||
let ref r2 = c;
|
||||
|
||||
assert_eq!(*r1, *r2);
|
||||
|
||||
// check the equality of the two address strings
|
||||
assert_eq!(get_addr(r1),get_addr(r2));
|
||||
}
|
||||
|
||||
// get memory address string
|
||||
fn get_addr(r: &char) -> String {
|
||||
format!("{:p}", r)
|
||||
}
|
||||
```
|
||||
|
||||
7.
|
||||
```rust
|
||||
fn main() {
|
||||
let s = String::from("hello");
|
||||
|
||||
let r1 = &s;
|
||||
let r2 = &s;
|
||||
|
||||
println!("{}, {}", r1, r2);
|
||||
}
|
||||
```
|
||||
|
||||
8.
|
||||
```rust
|
||||
fn main() {
|
||||
//fix error by modifying this line
|
||||
let mut s = String::from("hello, ");
|
||||
|
||||
borrow_object(&mut s)
|
||||
}
|
||||
|
||||
fn borrow_object(s: &mut String) {}
|
||||
```
|
||||
|
||||
9.
|
||||
```rust
|
||||
fn main() {
|
||||
let mut s = String::from("hello, ");
|
||||
|
||||
borrow_object(&s);
|
||||
|
||||
s.push_str("world");
|
||||
}
|
||||
|
||||
fn borrow_object(s: &String) {}
|
||||
```
|
||||
|
||||
10.
|
||||
```rust
|
||||
fn main() {
|
||||
let mut s = String::from("hello, ");
|
||||
|
||||
let r1 = &mut s;
|
||||
r1.push_str("world");
|
||||
let r2 = &mut s;
|
||||
r2.push_str("!");
|
||||
|
||||
// println!("{}",r1);
|
||||
}
|
||||
```
|
||||
|
||||
11.
|
||||
```rust
|
||||
fn main() {
|
||||
let mut s = String::from("hello, ");
|
||||
|
||||
let r1 = &mut s;
|
||||
let r2 = &mut s;
|
||||
|
||||
// add one line below to make a compiler error: cannot borrow `s` as mutable more than once at a time
|
||||
// you can't use r1 and r2 at the same time
|
||||
r1.push_str("world");
|
||||
}
|
||||
```
|
|
@ -3,7 +3,7 @@ The type of string literal `"hello, world"` is `&str`, e.g `let s: &str = "hello
|
|||
|
||||
|
||||
### str and &str
|
||||
🌟 We can't use `str` type in normal ways, but we can use `&str`
|
||||
1. 🌟 We can't use `str` type in normal ways, but we can use `&str`
|
||||
|
||||
```rust,editable
|
||||
|
||||
|
@ -14,7 +14,7 @@ fn main() {
|
|||
```
|
||||
|
||||
|
||||
🌟🌟🌟 We can only use `str` by boxed it, `&` can be used to convert `Box<str>` to `&str`
|
||||
2. 🌟🌟 We can only use `str` by boxed it, `&` can be used to convert `Box<str>` to `&str`
|
||||
|
||||
```rust,editable
|
||||
|
||||
|
@ -32,7 +32,7 @@ fn greetings(s: &str) {
|
|||
### String
|
||||
`String` type is defined in std and stored as a vector of bytes (Vec<u8>), but guaranteed to always be a valid UTF-8 sequence. String is heap allocated, growable and not null terminated.
|
||||
|
||||
🌟
|
||||
3. 🌟
|
||||
```rust,editable
|
||||
|
||||
// fill the blank
|
||||
|
@ -45,13 +45,13 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
🌟🌟🌟
|
||||
4. 🌟🌟🌟
|
||||
```rust,editable
|
||||
|
||||
// fix all errors without adding newline
|
||||
fn main() {
|
||||
let s = String::from("hello");
|
||||
s.push(',');`
|
||||
s.push(',');
|
||||
s.push(" world");
|
||||
s += "!".to_string();
|
||||
|
||||
|
@ -59,7 +59,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
🌟🌟 `replace` can be used to replace substring
|
||||
5. 🌟🌟 `replace` can be used to replace substring
|
||||
```rust,editable
|
||||
|
||||
// fill the blank
|
||||
|
@ -74,11 +74,11 @@ fn main() {
|
|||
|
||||
More `String` methods can be found under [String](https://doc.rust-lang.org/std/string/struct.String.html) module.
|
||||
|
||||
🌟🌟 You can only concat a `String` with `&str`, and `String`'s ownership can be moved to another variable
|
||||
6. 🌟🌟 You can only concat a `String` with `&str`, and `String`'s ownership can be moved to another variable
|
||||
|
||||
```rust,editable
|
||||
|
||||
// fix errors
|
||||
// fix errors without removing any line
|
||||
fn main() {
|
||||
let s1 = String::from("hello,");
|
||||
let s2 = String::from("world!");
|
||||
|
@ -91,7 +91,7 @@ fn main() {
|
|||
### &str and String
|
||||
Opsite to the seldom using of `str`, `&str` and `String` are used everywhere!
|
||||
|
||||
🌟🌟 `&str` can be converted to `String` in two ways
|
||||
7. 🌟🌟 `&str` can be converted to `String` in two ways
|
||||
```rust,editable
|
||||
|
||||
// fix error with at lest two solutions
|
||||
|
@ -105,7 +105,7 @@ fn greetings(s: String) {
|
|||
}
|
||||
```
|
||||
|
||||
🌟🌟 We can use `String::from` or `to_string` to convert a `&str` to `String`
|
||||
8. 🌟🌟 We can use `String::from` or `to_string` to convert a `&str` to `String`
|
||||
|
||||
```rust,editable
|
||||
|
||||
|
@ -117,7 +117,7 @@ fn main() {
|
|||
```
|
||||
|
||||
### string escapes
|
||||
🌟
|
||||
9. 🌟
|
||||
```rust,editable
|
||||
fn main() {
|
||||
// You can use escapes to write bytes by their hexadecimal values
|
||||
|
@ -140,7 +140,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
🌟🌟🌟 Sometimes there are just too many characters that need to be escaped or it's just much more convenient to write a string out as-is. This is where raw string literals come into play.
|
||||
10. 🌟🌟🌟 Sometimes there are just too many characters that need to be escaped or it's just much more convenient to write a string out as-is. This is where raw string literals come into play.
|
||||
|
||||
```rust,editable
|
||||
|
||||
|
@ -211,7 +211,7 @@ fn main() {
|
|||
A more detailed listing of the ways to write string literals and escape characters is given in the ['Tokens' chapter](https://doc.rust-lang.org/reference/tokens.html) of the Rust Reference.
|
||||
|
||||
### string index
|
||||
🌟🌟 You can't use index to access a char in a string, but you can use slice `&s1[start..end]`.
|
||||
11. 🌟🌟🌟 You can't use index to access a char in a string, but you can use slice `&s1[start..end]`.
|
||||
|
||||
```rust,editable
|
||||
|
||||
|
@ -226,7 +226,7 @@ fn main() {
|
|||
```
|
||||
|
||||
### operate on UTF8 string
|
||||
🌟
|
||||
12. 🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# Reference and Borrowing
|
||||
|
||||
### Reference
|
||||
🌟
|
||||
1. 🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
|
@ -9,11 +9,11 @@ fn main() {
|
|||
// fill the blank
|
||||
let p = __;
|
||||
|
||||
println!("the memory address of x is {:p}", p); // output: 0x16fa3ac84
|
||||
println!("the memory address of x is {:p}", p); // one possible output: 0x16fa3ac84
|
||||
}
|
||||
```
|
||||
|
||||
🌟
|
||||
2. 🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
|
@ -25,7 +25,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
🌟
|
||||
3. 🌟
|
||||
```rust,editable
|
||||
|
||||
// fix error
|
||||
|
@ -38,20 +38,22 @@ fn main() {
|
|||
fn borrow_object(s: &String) {}
|
||||
```
|
||||
|
||||
🌟
|
||||
4. 🌟
|
||||
```rust,editable
|
||||
|
||||
// fix error
|
||||
fn main() {
|
||||
let mut s = String::from("hello, ");
|
||||
|
||||
borrow_object(&s)
|
||||
push_str(s)
|
||||
}
|
||||
|
||||
fn borrow_object(s: &mut String) {}
|
||||
fn push_str(s: &mut String) {
|
||||
s.push_str("world")
|
||||
}
|
||||
```
|
||||
|
||||
🌟🌟
|
||||
5. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
|
@ -67,7 +69,7 @@ fn main() {
|
|||
#### ref
|
||||
`ref` can be used to take references to a value, similar to `&`.
|
||||
|
||||
🌟🌟🌟
|
||||
6. 🌟🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
|
@ -90,7 +92,7 @@ fn get_addr(r: &char) -> String {
|
|||
```
|
||||
|
||||
### Borrowing rules
|
||||
🌟
|
||||
7. 🌟
|
||||
```rust,editable
|
||||
|
||||
// remove something to make it work
|
||||
|
@ -106,7 +108,7 @@ fn main() {
|
|||
```
|
||||
|
||||
#### Mutablity
|
||||
🌟 Error: Borrow a immutable object as mutable
|
||||
8. 🌟 Error: Borrow a immutable object as mutable
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
|
@ -119,7 +121,7 @@ fn main() {
|
|||
fn borrow_object(s: &mut String) {}
|
||||
```
|
||||
|
||||
🌟🌟 Ok: Borrow a mutable object as immutable
|
||||
9. 🌟🌟 Ok: Borrow a mutable object as immutable
|
||||
```rust,editable
|
||||
|
||||
// this code has no errors!
|
||||
|
@ -135,7 +137,7 @@ fn borrow_object(s: &String) {}
|
|||
```
|
||||
|
||||
### NLL
|
||||
🌟🌟
|
||||
10. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
// comment one line to make it work
|
||||
|
@ -151,7 +153,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
🌟🌟🌟
|
||||
11. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -14,7 +14,7 @@ fn main() {
|
|||
```
|
||||
|
||||
|
||||
🌟🌟🌟 如果要使用 `str` 类型,只能配合 `Box`。 `&` 可以用来将 `Box<str>` 转换为 `&str` 类型
|
||||
🌟🌟 如果要使用 `str` 类型,只能配合 `Box`。 `&` 可以用来将 `Box<str>` 转换为 `&str` 类型
|
||||
|
||||
```rust,editable
|
||||
|
||||
|
@ -51,7 +51,7 @@ fn main() {
|
|||
// 修复所有错误,并且不要新增代码行
|
||||
fn main() {
|
||||
let s = String::from("hello");
|
||||
s.push(',');`
|
||||
s.push(',');
|
||||
s.push(" world");
|
||||
s += "!".to_string();
|
||||
|
||||
|
@ -79,7 +79,7 @@ fn main() {
|
|||
|
||||
```rust,editable
|
||||
|
||||
// 修复所有错误
|
||||
// 修复所有错误,不要删除任何一行代码
|
||||
fn main() {
|
||||
let s1 = String::from("hello,");
|
||||
let s2 = String::from("world!");
|
||||
|
|
|
@ -45,10 +45,12 @@ fn borrow_object(s: &String) {}
|
|||
fn main() {
|
||||
let mut s = String::from("hello, ");
|
||||
|
||||
borrow_object(&s)
|
||||
push_str(s)
|
||||
}
|
||||
|
||||
fn borrow_object(s: &mut String) {}
|
||||
fn push_str(s: &mut String) {
|
||||
s.push_str("world")
|
||||
}
|
||||
```
|
||||
|
||||
🌟🌟
|
||||
|
@ -151,7 +153,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
🌟🌟🌟
|
||||
🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
|
|
Loading…
Reference in New Issue