Rust-String Flashcards

1
Q

Classifications of the String data type

A

String literal(&str)
String Object(String)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When is the String literal used?

A

String literals are used when the value of a string is know at compile time.

String literals are a set of characters, which are hardcoded into a variable.

String literals are also known as string slices

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give an example of a string literal declaration

A

let company:&str = “TutorialsPoint”;
let first_name:&str = “Prince”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe String object

A

The String object type can be used to represent string values that are provided at runtime

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where is the string object allocated?

A

Heap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do create an empty string object

A

String::new();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Give an example of a program using the string object

A

fn main(){
let empty_string = String::new();
println!(“length is {}”,empty_string.len());

let content_string = String::from(“TutorialsPoint”);
println!(“length is {}”,content_string.len());
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Mention common methods of the string object

A

new()
to_string()
replace()
push()
push_str()
len()
trim()
split_whitespace()
split()
chars()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe the to_string()

A

Converts the given value to a String.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe replace()

A

Replaces all matches of a pattern with another string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe push() and push_str()

A

push() - Appends the given char to the end of this String.

push_str() - Appends a given string slice onto the end of this String.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Describe split() and chars()

A

split() –Returns an iterator over substrings of this string slice, separated by characters matched by a pattern.

chars() – Returns an iterator over the chars of a string slice.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give an example of the string object with using at least two of its method

A

fn main(){
let mut z = String::new();
z.push_str(“hello”);
println!(“{}”,z);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

You can convert a string literal to a string object how

A

let name = “Prince”.to_string();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you use the replace() method;

A

let sentence = “Ibekwe is 19”;
sentence.to_string();
let sentence2 = sentence.to_replace(“Ibekwe”,”Prince”)

17
Q

Give an example on how to use split_whitespace();

A

fn main(){
let msg = “Tutorials Point has good tutorials”.to_string();
let mut i = 1;

for token in msg.split_whitespace(){
println!(“token {} {}”,i,token);
i+=1;
}
}

//token 1 Tutorials
//token 2 Point
//token 3 has
//token 4 good
//token 5 tutorials