mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
add solutions for string.md
This commit is contained in:
@ -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");
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user