Practice 50 Ownership, Borrowing, and Lifetimes Coding Questions, TechnoVlogs

Practice 50 Ownership, Borrowing, and Lifetimes Coding Questions


Q1. Write a Rust program to demonstrate transferring ownership of a variable from one function to another.  
Input:  
String: "Hello, Rust!"  
Expected Output:  
String in function: "Hello, Rust!"

Q2. Write a Rust program to illustrate the concept of borrowing by passing a reference to a function.  
Input:  
Number: 42  
Expected Output:  
Number is: 42

Q3. Write a Rust program to show how mutable references can be used to modify a variable within a function.  
Input:  
Initial value: 10  
Expected Output:  
Modified value: 20

Q4. Write a Rust program to demonstrate the restriction of having multiple mutable references.  
Input:  
None  
Expected Output:  
Error: cannot borrow as mutable more than once

Q5. Write a Rust program to create a function that takes ownership of a string and returns its length.  
Input:  
String: "Ownership"  
Expected Output:  
Length: 9

Q6. Write a Rust program to show how to use & to create a reference without transferring ownership.  
Input:  
Value: 100  
Expected Output:  
Borrowed value: 100

Q7. Write a Rust program to demonstrate ownership rules in a for loop when iterating over a vector.  
Input:  
Vector: [1, 2, 3]  
Expected Output:  
Cannot use vector after moving ownership

Q8. Write a Rust program to illustrate how slicing works without transferring ownership.  
Input:  
String: "Rustacean"  
Expected Output:  
Slice: "Rust"

Q9. Write a Rust program to show how ownership affects closure behavior in Rust.  
Input:  
Value: 5  
Expected Output:  
Closure uses value: 5

Q10. Write a Rust program to demonstrate shared references using multiple & operators.  
Input:  
Value: 50  
Expected Output:  
Shared reference: 50

Q11. Write a Rust program to use a Vec and borrow its elements using references.  
Input:  
Vector: [10, 20, 30]  
Expected Output:  
Values: 10, 20, 30

Q12. Write a Rust program to show how to fix a dangling reference error using lifetimes.  
Input:  
None  
Expected Output:  
Fixed with explicit lifetimes

Q13. Write a Rust program to demonstrate using String::clone() to explicitly create a new owner.  
Input:  
String: "Clone me"  
Expected Output:  
Original: "Clone me", Cloned: "Clone me"

Q14. Write a Rust program to show how borrowing rules apply in nested functions.  
Input:  
Value: 7  
Expected Output:  
Borrowed in inner function: 7

Q15. Write a Rust program to illustrate ownership transfer in a tuple.  
Input:  
Tuple: (5, "Rust")  
Expected Output:  
Ownership moved to function

Q16. Write a Rust program to use mutable references in a loop to update elements in a vector.  
Input:  
Vector: [1, 2, 3]  
Expected Output:  
Updated vector: [2, 4, 6]

Q17. Write a Rust program to demonstrate returning a reference from a function with appropriate lifetimes.  
Input:  
Array: [10, 20, 30]  
Expected Output:  
First element: 10

Q18. Write a Rust program to show how ownership works with structs.  
Input:  
Struct: { x: 10, y: 20 }  
Expected Output:  
Ownership moved to function

Q19. Write a Rust program to demonstrate the difference between copying and moving ownership for primitive types.  
Input:  
Integer: 42  
Expected Output:  
Both values can be used after copy

Q20. Write a Rust program to use Rc (Reference Counting) to enable multiple ownership of a value.  
Input:  
Value: 15  
Expected Output:  
Count after clone: 2

Q21. Write a Rust program to demonstrate the use of Box<T> for heap allocation and ownership transfer.  
Input:  
Value: 42  
Expected Output:  
Boxed value: 42

Q22. Write a Rust program to show how to use RefCell<T> for runtime borrow checking.  
Input:  
Value: 10  
Expected Output:  
Borrowed value: 10

Q23. Write a Rust program to illustrate shared references with Rc in a vector.  
Input:  
Vector: [1, 2, 3]  
Expected Output:  
Shared reference count: 1

Q24. Write a Rust program to demonstrate returning ownership of a value from a function.  
Input:  
String: "Return me"  
Expected Output:  
Returned string: "Return me"

Q25. Write a Rust program to create a function that accepts both mutable and immutable references.  
Input:  
Value: 20  
Expected Output:  
Immutable: 20, Mutable updated to: 40

Q26. Write a Rust program to show how ownership and borrowing apply to arrays.  
Input:  
Array: [10, 20, 30]  
Expected Output:  
First element: 10

Q27. Write a Rust program to demonstrate how String ownership behaves when passed into and returned from a function.  
Input:  
String: "Ownership"  
Expected Output:  
Returned string: "Ownership"

Q28. Write a Rust program to illustrate the concept of borrowing multiple immutable references.  
Input:  
Values: [5, 10]  
Expected Output:  
References: 5, 10

Q29. Write a Rust program to demonstrate how slices avoid transferring ownership.  
Input:  
String: "Rustacean"  
Expected Output:  
Slice: "Rust"

Q30. Write a Rust program to show how ownership works with vectors.  
Input:  
Vector: [1, 2, 3]  
Expected Output:  
Moved vector: [1, 2, 3]

Q31. Write a Rust program to demonstrate the use of lifetimes to prevent dangling references.  
Input:  
None  
Expected Output:  
Lifetime ensured no dangling references

Q32. Write a Rust program to illustrate borrowing rules for function parameters.  
Input:  
Value: 100  
Expected Output:  
Borrowed value: 100

Q33. Write a Rust program to create a function that clones a vector before modifying it.  
Input:  
Vector: [1, 2, 3]  
Expected Output:  
Original: [1, 2, 3], Modified clone: [2, 4, 6]

Q34. Write a Rust program to demonstrate ownership transfer with structs.  
Input:  
Struct: { x: 10, y: 20 }  
Expected Output:  
Moved struct: { x: 10, y: 20 }

Q35. Write a Rust program to use String::from to create a new string without affecting the original.  
Input:  
String: "Rust"  
Expected Output:  
Original: "Rust", New: "Rust"

Q36. Write a Rust program to demonstrate using &mut for borrowing and modifying a value.  
Input:  
Value: 10  
Expected Output:  
Modified value: 20

Q37. Write a Rust program to show how to safely borrow from a vector using lifetimes.  
Input:  
Vector: [10, 20, 30]  
Expected Output:  
Borrowed: 10

Q38. Write a Rust program to demonstrate how ownership works with Option<T>.  
Input:  
Option: Some(42)  
Expected Output:  
Ownership moved: 42

Q39. Write a Rust program to use Cow (Copy on Write) for efficient borrowing and cloning.  
Input:  
String: "Hello"  
Expected Output:  
Borrowed: "Hello"

Q40. Write a Rust program to illustrate the difference between stack and heap memory in ownership.  
Input:  
None  
Expected Output:  
Stack value: 5, Heap value: "Hello"

Q41. Write a Rust program to demonstrate how mutable and immutable references coexist with constraints.  
Input:  
Value: 100  
Expected Output:  
Mutable and immutable coexistence not allowed

Q42. Write a Rust program to use a lifetime to tie a returned reference to an input reference.  
Input:  
String: "Lifetime"  
Expected Output:  
Returned slice: "Life"

Q43. Write a Rust program to demonstrate ownership transfer using a HashMap.  
Input:  
HashMap: {"a": 1, "b": 2}  
Expected Output:  
Moved HashMap: {"a": 1, "b": 2}

Q44. Write a Rust program to use Rc with RefCell for interior mutability and shared ownership.  
Input:  
Value: 50  
Expected Output:  
Shared ownership and mutation allowed

Q45. Write a Rust program to demonstrate the concept of lifetimes in closures.  
Input:  
Value: 5  
Expected Output:  
Closure holds value: 5

Q46. Write a Rust program to create a function that borrows an integer and returns its square.  
Input:  
Value: 4  
Expected Output:  
Square: 16

Q47. Write a Rust program to illustrate lifetime annotations in a struct.  
Input:  
Struct: { value: "Hello" }  
Expected Output:  
Struct value: Hello

Q48. Write a Rust program to use Cell for interior mutability of primitive types.  
Input:  
Value: 20  
Expected Output:  
Updated value: 40

Q49. Write a Rust program to show ownership behavior in a HashSet.  
Input:  
HashSet: {1, 2, 3}  
Expected Output:  
HashSet moved

Q50. Write a Rust program to demonstrate ownership and borrowing in enums.  
Input:  
Enum: Some(100)  
Expected Output:  
Borrowed value: 100

Share on Social Media