Difference between revisions of "Rust-Programming-Language/C2/Strings/English"
| Line 37: | Line 37: | ||
|| The String data type in Rust can be classified as | || The String data type in Rust can be classified as | ||
| − | *String Literal(&str) | + | *String Literal(&str) that is string slice and |
*String Object(String) | *String Object(String) | ||
| Line 58: | Line 58: | ||
|| | || | ||
| − | *A string slice is a reference to a part of | + | * A string slice is a reference to a part of string. |
* It's represented by the type''' &str ''' | * 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. | + | * A string slice cannot be modified as it points to a part of memory that holds the string. |
|- | |- | ||
| − | || | + | || Cursor on the panel. |
|| Let us open the '''visual code editor.''' | || 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.''' | || In the menu bar, click on '''Terminal''' and select '''New Terminal.''' | ||
| Line 84: | Line 83: | ||
|- | |- | ||
|| | || | ||
| − | || In the '''main.rs '''file, copy and paste the '''code''' from the | + | || In the '''main.rs '''file, copy and paste the '''code''' from the Codefile. |
|- | |- | ||
|| '''code example:''' | || '''code example:''' | ||
| Line 101: | Line 100: | ||
'''String::from '''creates a new String object from a string literal ("Hello, world!"). | '''String::from '''creates a new String object from a string literal ("Hello, world!"). | ||
| − | Variable '''x '''takes a slice of the string '''s'''. | + | Variable '''x ''' takes a slice of the string '''s'''. |
0 to 5 is a '''range''' that means "from index 0 up to index 5". | 0 to 5 is a '''range''' that means "from index 0 up to index 5". | ||
| Line 107: | Line 106: | ||
Note that the slicing excludes the end index character. | 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. | + | 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". | So, it gives you the substring "Hello". | ||
| Line 118: | Line 119: | ||
In the terminal, type '''cargo run''' | In the terminal, type '''cargo run''' | ||
| − | We can see ''' Hello '''is printed as the output. | + | We can see ''' Hello ''' is printed as the output. |
|- | |- | ||
|| | || | ||
| Line 128: | Line 129: | ||
|| | || | ||
| − | *String Object is mutable, growable and heap allocated string type. | + | * String Object is mutable, growable and heap allocated string type. |
| − | *It is commonly used when the strings can be modified and dynamically sized. | + | * It is commonly used when the strings can be modified and dynamically sized. |
* It automatically frees the memory when it goes out of scope. | * It automatically frees the memory when it goes out of scope. | ||
| Line 209: | Line 210: | ||
|| 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. | + | '''Push() ''' function appends the given char to the end of this String. |
Save the program. | Save the program. | ||
| Line 266: | Line 267: | ||
|- | |- | ||
|| | || | ||
| − | |||
fn main() { | fn main() { | ||
Latest revision as of 11:56, 1 October 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
|
|
| Cursor on the panel. | 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 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. Save the program. 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. Save the program. 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. Save the program. 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. Save the program. 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. Save the program. 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. |