Difference between revisions of "Rust-Programming-Language/C2/Strings/English"
(Created page with "{| border="1" |- || '''Visual Cue''' || '''Narration''' |- || '''Slide 1''' || <span style="color:#000000;">Welcome to the Spoken Tutorial on </span><span style="color:#000000...") |
|||
| Line 5: | Line 5: | ||
|- | |- | ||
|| '''Slide 1''' | || '''Slide 1''' | ||
| − | || | + | || Welcome to the Spoken Tutorial on '''Strings '''in '''Rust'''. |
| − | |- | + | |- |
| − | || | + | || '''Slide 2''': |
| − | + | '''Learning Objectives''' | |
| − | || | + | || In this tutorial, we will learn about |
| − | * | + | * String data type |
| − | |- | + | |- |
| − | || | + | || '''Slide 3''': |
| − | + | '''System Requirements''' | |
|| | || | ||
| − | * | + | * To record this tutorial I’m using the following setup. |
| − | |- | + | |- |
| − | || | + | || '''Slide 5''': |
| + | |||
| + | '''Code Files''' | ||
| − | |||
| − | |||
| − | |||
'''stringsdemo.rs''' | '''stringsdemo.rs''' | ||
| − | * | + | || |
| + | * The following code file is required to practise this tutorial | ||
| − | + | * This file is provided in the code files link of this tutorial page | |
| − | + | ||
| − | + | |- | |
| + | || '''Slide 6:''' | ||
| − | + | '''Strings''' | |
| − | + | ||
| − | + | ||
| − | | | + | || The String data type in Rust can be classified as |
| − | + | *String Literal(&str) i.e string slice and | |
| + | *String Object(String) | ||
| − | + | |- | |
| + | || '''Slide 7:''' | ||
| + | |||
| + | '''String Literal''' | ||
|| | || | ||
| − | * | + | * A string literal is a statically allocated string stored in the program's binary |
| − | * | + | * It is written using '''double quotes (")''' |
| − | * | + | * String literals are also known as '''string slices.''' |
| − | |- | + | |- |
| − | || | + | || '''Slide 8:''' |
| − | + | '''String slice''' | |
| + | |||
| + | * '''Eg: let name: &str = "Rust Program";''' | ||
|| | || | ||
| − | * | + | *A string slice is a reference to a part of a string. |
| − | * | + | * It's represented by the type''' &str ''' |
| − | * | + | *A string slice cannot be modified as it points to a part of memory that holds the string. |
| − | |- | + | |- |
| − | | | + | || |
| − | || | + | || Let us open the '''visual code editor.''' |
| − | |- | + | |- |
| − | || | + | || click on '''terminal''' and select '''New Terminal''' |
| − | || | + | || In the menu bar, click on '''Terminal''' and select '''New Terminal.''' |
| − | |- | + | |- |
|| '''> cd''' '''Desktop'''/'''MyRustProject''' | || '''> cd''' '''Desktop'''/'''MyRustProject''' | ||
| Line 72: | Line 76: | ||
'''In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> stringsdemo''' | '''In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> stringsdemo''' | ||
| − | |||
| − | + | || Go to our working directory '''MyRustProject''' as explained earlier. | |
| − | + | Type the command '''cargo new stringsdemo''' and press''' Enter'''. | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | Open the created project as shown. | |
| + | |- | ||
| + | || | ||
| + | || In the '''main.rs '''file, copy and paste the '''code''' from the codefile. | ||
| + | |- | ||
| + | || '''code example:''' | ||
| − | + | fn main() { | |
| − | + | let s = String::from("Hello, world!"); | |
| − | + | let x = &s[0..5]; // A slice of the first 5 characters | |
| − | + | println!("{}", x); // output: Hello | |
| − | + | ||
| − | + | } | |
| + | || We have created a heap-allocated string '''s''' . | ||
| − | + | '''String::from '''creates a new String object from a string literal ("Hello, world!"). | |
| − | + | Variable '''x '''takes a slice of the string '''s'''. | |
| − | + | 0 to 5 is a '''range''' that means "from index 0 up to index 5". | |
| − | + | Note that the slicing excludes the end index character. | |
| − | + | ||
| − | + | ||
| − | + | As '''x''' references part of '''s''', it doesn’t own the '''String'''. So no new memory is allocated to it. | |
| − | + | So, it gives you the substring "Hello". | |
| − | + | ||
| − | + | Save the program. | |
| + | |- | ||
| + | || Point to the output. | ||
| + | || Let us run the program and check the output. | ||
| − | + | In the menu bar, click on Terminal and select '''New Terminal.''' | |
| − | + | In the terminal, type '''cargo run''' | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | We can see ''' Hello '''is printed as the output. | |
| + | |- | ||
| + | || | ||
| + | || Next we will see about '''String Objects.''' | ||
| + | |- | ||
| + | || Slide 9: | ||
| + | |||
| + | String Objects | ||
|| | || | ||
| − | * | + | *String Object is mutable, growable and heap allocated string type. |
| − | * | + | *It is commonly used when the strings can be modified and dynamically sized. |
| − | * | + | * It automatically frees the memory when it goes out of scope. |
| − | |- | + | |- |
| − | | | + | || |
| − | || | + | || Next we will see some of the common methods used with string objects. |
| − | + | Copy and paste the code from the code file for each function in the below demonstration. | |
| − | |- | + | |- |
| − | || | + | || fn main() { |
| − | + | let s = String::from("Hello World"); | |
| − | + | println!("Length: {}", s.len()); // Outputs: 11 | |
} | } | ||
> cargo run | > cargo run | ||
| − | |||
| − | + | || First we will see how to use the '''len()''' function. | |
| − | + | The '''len()''' function returns the total number of characters in a string, including the spaces. | |
| − | Run the program. We can see the length of the string printed as 11. | + | Save the program. |
| − | |- | + | |
| + | Run the program. | ||
| + | |||
| + | We can see the length of the string printed as 11. | ||
| + | |- | ||
|| fn main() { | || fn main() { | ||
| − | + | let mut s = String::from("Hello"); | |
| − | + | s.clear(); | |
| − | + | println!("Cleared string: {}", s); // Outputs an empty line | |
} | } | ||
| − | || | + | || Next we will see about the '''clear()''' function. |
| − | + | The ''' clear()''' function clears the string and returns an empty string. | |
Run the program. | Run the program. | ||
We can see the empty string as output. | We can see the empty string as output. | ||
| − | |- | + | |- |
|| fn main(){ | || fn main(){ | ||
| − | + | let s = String::from("I love Rust"); | |
| − | + | let new_s = s.replace("love", "enjoy"); | |
| − | + | println!("{}", new_s); } | |
| − | + | // Outputs: I enjoy Rust | |
| − | + | ||
| − | This program | + | || Next we will check how the '''replace''' function works. |
| + | |||
| + | This program replaces all matches of a pattern with another string. | ||
Let us run the program and verify the output. | Let us run the program and verify the output. | ||
| Line 186: | Line 195: | ||
We can see that the string “love” is replaced with “enjoy”. | We can see that the string “love” is replaced with “enjoy”. | ||
| − | |- | + | |- |
| − | || | + | || fn main(){ |
| − | + | let mut s = "Tutorial".to_string(); | |
| − | + | s.push('s'); | |
| − | + | println!("{}",s); | |
| − | + | } | |
|| Let us see how the '''Push''' function works. | || Let us see how the '''Push''' function works. | ||
| − | + | '''Push() '''function appends the given char to the end of this String. | |
Run the program . | Run the program . | ||
In the output, “s” is added at the end of the string “tutorial”. | In the output, “s” is added at the end of the string “tutorial”. | ||
| − | |- | + | |- |
| − | || fn | + | || fn main(){ |
| − | + | let s = String::from("Hello, Rust, World"); | |
| − | + | for item in s.split(", ") { | |
| − | + | println!("{}", item); | |
| − | + | } | |
} | } | ||
|| Next we will see about '''split''' function. | || Next we will see about '''split''' function. | ||
| − | It | + | It splits the string, whenever it encounters a comma and space. |
It returns an '''iterator''' over substrings separated by that pattern. | It returns an '''iterator''' over substrings separated by that pattern. | ||
| − | The '''for '''loop consumes the iterator and prints each substring on a new line. | + | The '''for ''' loop consumes the iterator and prints each substring on a new line. |
In the terminal, run the program and check the output. | In the terminal, run the program and check the output. | ||
We can see each string slice printed on a new line. | We can see each string slice printed on a new line. | ||
| − | |- | + | |- |
| − | || | + | || fn main() { |
| − | + | let str = String::from("Hello"); | |
| − | + | // Loop through each character in a string using chars() method | |
| − | + | for char in str.chars() { | |
| − | + | println!("{}", char); | |
| − | + | } | |
| − | + | } | |
|| This program shows how to use the '''chars()''' method. | || This program shows how to use the '''chars()''' method. | ||
| − | + | We can use the '''chars() '''method of the string type to iterate over a string. | |
Let us check the output. | Let us check the output. | ||
We can see each character of the string is printed in a separate line. | We can see each character of the string is printed in a separate line. | ||
| − | |- | + | |- |
| − | || | + | || |
| − | + | fn main() { | |
| − | + | let fullname = " Spoken Tutorials \r\n"; | |
| − | + | println!("Before trim "); | |
| − | + | println!("length is {}",fullname.len()); | |
| − | + | println!(); | |
| − | + | println!("After trim "); | |
| − | + | println!("length is {}",fullname.trim().len()); | |
| − | + | } | |
|| Next is about the '''trim''' function. | || Next is about the '''trim''' function. | ||
| − | + | The '''trim()''' function removes leading and trailing spaces in a string. | |
| − | + | This will not remove the inline spaces. | |
Save the program. | Save the program. | ||
| Line 275: | Line 284: | ||
Analyse the output. | Analyse the output. | ||
| − | |- | + | |- |
|| Slide: | || Slide: | ||
Additional reading material | Additional reading material | ||
|| | || | ||
| − | * | + | * Please go through the additional reading material provided in the link of this tutorial. |
| − | * | + | * You will learn more about string memory allocation concept and other string methods. |
| − | |- | + | |- |
|| Slide: | || Slide: | ||
| Line 290: | Line 299: | ||
In this tutorial, we learnt about | In this tutorial, we learnt about | ||
| − | * | + | * String data type |
| − | || | + | || This brings us to the end of this tutorial. |
| − | + | Let us summarize. | |
| − | |- | + | |- |
|| Slide: | || Slide: | ||
| Line 309: | Line 318: | ||
} | } | ||
| − | || | + | || As an assignment do the following: |
| − | * | + | * Run the above code |
| − | * | + | * Understand the difference between '''push''' and '''push_str '''function. |
| − | |- | + | |- |
|| Thank You | || Thank You | ||
|| Thanks for joining. | || Thanks for joining. | ||
|- | |- | ||
|} | |} | ||
| − | |||
Revision as of 18:18, 19 September 2025
| Visual Cue | Narration |
| Slide 1 | Welcome to the Spoken Tutorial on Strings in Rust. |
| Slide 2:
Learning Objectives |
In this tutorial, we will learn about
|
| Slide 3:
System Requirements |
|
| Slide 5:
Code Files stringsdemo.rs |
|
| Slide 6:
Strings |
The String data type in Rust can be classified as
|
| Slide 7:
String Literal |
|
| Slide 8:
String slice
|
|
| Let us open the visual code editor. | |
| click on terminal and select New Terminal | In the menu bar, click on Terminal and select New Terminal. |
| > cd Desktop/MyRustProject
> cargo new stringsdemo In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> stringsdemo |
Go to our working directory MyRustProject as explained earlier.
Type the command cargo new stringsdemo and press Enter. Open the created project as shown. |
| In the main.rs file, copy and paste the code from the codefile. | |
| code example:
fn main() { let s = String::from("Hello, world!"); let x = &s[0..5]; // A slice of the first 5 characters println!("{}", x); // output: Hello } |
We have created a heap-allocated string s .
String::from creates a new String object from a string literal ("Hello, world!"). Variable x takes a slice of the string s. 0 to 5 is a range that means "from index 0 up to index 5". Note that the slicing excludes the end index character. As x references part of s, it doesn’t own the String. So no new memory is allocated to it. So, it gives you the substring "Hello". Save the program. |
| Point to the output. | Let us run the program and check the output.
In the menu bar, click on Terminal and select New Terminal. In the terminal, type cargo run We can see Hello is printed as the output. |
| Next we will see about String Objects. | |
| Slide 9:
String Objects |
|
| Next we will see some of the common methods used with string objects.
Copy and paste the code from the code file for each function in the below demonstration. | |
| fn main() {
let s = String::from("Hello World"); println!("Length: {}", s.len()); // Outputs: 11 } > cargo run |
First we will see how to use the len() function.
The len() function returns the total number of characters in a string, including the spaces. Save the program. Run the program. We can see the length of the string printed as 11. |
| fn main() {
let mut s = String::from("Hello"); s.clear(); println!("Cleared string: {}", s); // Outputs an empty line } |
Next we will see about the clear() function.
The clear() function clears the string and returns an empty string. Run the program. We can see the empty string as output. |
| fn main(){
let s = String::from("I love Rust"); let new_s = s.replace("love", "enjoy"); println!("{}", new_s); } // Outputs: I enjoy Rust |
Next we will check how the replace function works.
This program replaces all matches of a pattern with another string. Let us run the program and verify the output. We can see that the string “love” is replaced with “enjoy”. |
| fn main(){
let mut s = "Tutorial".to_string(); s.push('s'); println!("{}",s); } |
Let us see how the Push function works.
Push() function appends the given char to the end of this String. Run the program . In the output, “s” is added at the end of the string “tutorial”. |
| fn main(){
let s = String::from("Hello, Rust, World"); for item in s.split(", ") { println!("{}", item); } } |
Next we will see about split function.
It splits the string, whenever it encounters a comma and space. It returns an iterator over substrings separated by that pattern. The for loop consumes the iterator and prints each substring on a new line. In the terminal, run the program and check the output. We can see each string slice printed on a new line. |
| fn main() {
let str = String::from("Hello"); // Loop through each character in a string using chars() method for char in str.chars() { println!("{}", char); } } |
This program shows how to use the chars() method.
We can use the chars() method of the string type to iterate over a string. Let us check the output. We can see each character of the string is printed in a separate line. |
|
fn main() { let fullname = " Spoken Tutorials \r\n"; println!("Before trim "); println!("length is {}",fullname.len()); println!(); println!("After trim "); println!("length is {}",fullname.trim().len()); } |
Next is about the trim function.
The trim() function removes leading and trailing spaces in a string. This will not remove the inline spaces. Save the program. Analyse the output. |
| Slide:
Additional reading material |
|
| Slide:
Summary In this tutorial, we learnt about
|
This brings us to the end of this tutorial.
Let us summarize. |
| Slide:
Assignment fn main(){ let mut title = " Spoken Tutorial".to_string(); title.push_str(" Project"); println!("{}",title); } |
As an assignment do the following:
|
| Thank You | Thanks for joining. |