https://github.com/yomnaali22/rust_simple_programs
Rise in task: basic programs that uses ownership concepts, enums, structs and error handling
https://github.com/yomnaali22/rust_simple_programs
enums impl input ownership rust struct
Last synced: 5 months ago
JSON representation
Rise in task: basic programs that uses ownership concepts, enums, structs and error handling
- Host: GitHub
- URL: https://github.com/yomnaali22/rust_simple_programs
- Owner: Yomnaali22
- Created: 2025-02-24T16:58:58.000Z (12 months ago)
- Default Branch: master
- Last Pushed: 2025-03-16T16:48:36.000Z (11 months ago)
- Last Synced: 2025-07-13T02:26:44.070Z (7 months ago)
- Topics: enums, impl, input, ownership, rust, struct
- Language: Rust
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 📌 Rust String Concatenation: Ownership & Borrowing
## 📖 Task Details
In this task, students will create a simple Rust program that demonstrates the concepts of **ownership, borrowing, and references**. The program will take two strings as input, concatenate them, and then print the result **without violating any ownership rules**.
---
## 🚀 Program Code
```rust
fn concatenate_strings(str1: &str, str2: &str) -> String {
let mut result = String::new(); // mutable variable
result.push_str(str1);
result.push_str(str2);
return result;
}
fn main() {
let string1: String = String::from("hello");
let string2: String = String::from(" world");
let concatenated_string = concatenate_strings(&string1, &string2); // & for borrowing
println!("{}", concatenated_string);
}
```
---
## 📝 Explanation
- The function **`concatenate_strings`** takes two **borrowed** string slices (`&str`).
- This prevents **ownership transfer**, allowing `string1` and `string2` to still be used after the function call.
- A **new `String`** is created inside the function, modified with `push_str()`, and returned.
- In `main()`, the function is called using **borrowed references** (`&string1` and `&string2`).
- This ensures **no ownership violations** while efficiently concatenating the strings.
---
## ✅ Expected Output
```
hello world
```
---
## 📌 Key Concepts Demonstrated
- **Ownership**: The function does not take ownership of `string1` and `string2`, allowing them to be reused.
- **Borrowing & References**: The function parameters use `&str`, enabling safe and efficient string handling.
- **String Manipulation**: Using `push_str()` to append borrowed string slices to a mutable `String`.
---
## 🔥 Running the Program
### 1️⃣ Ensure you have Rust installed:
```sh
rustc --version
```
If Rust is not installed, install it using:
```sh
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```
### 2️⃣ Compile and Run the Program:
```sh
rustc main.rs
./main
```
OR using Cargo:
```sh
cargo run
```
---