From c150e0be252946ea439325079604fe68326fd6db Mon Sep 17 00:00:00 2001 From: Alex Bondoc Date: Thu, 9 Jan 2025 19:30:17 +0200 Subject: [PATCH] Fixed issue 534 --- lessons/en/chapter_5.yaml | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lessons/en/chapter_5.yaml b/lessons/en/chapter_5.yaml index 917c8aeec..d7cb8789d 100644 --- a/lessons/en/chapter_5.yaml +++ b/lessons/en/chapter_5.yaml @@ -43,18 +43,30 @@ https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=struct%20Bar%20%7B%0A%20%20%20%20x%3A%20i32%2C%0A%7D%0A%0Astruct%20Foo%20%7B%0A%20%20%20%20bar%3A%20Bar%2C%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20foo%20%3D%20Foo%20%7B%20bar%3A%20Bar%20%7B%20x%3A%2042%20%7D%20%7D%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20foo.bar.x)%3B%0A%20%20%20%20%2F%2F%20foo%20is%20dropped%20first%0A%20%20%20%20%2F%2F%20then%20foo.bar%20is%20dropped%0A%7D%0A - title: Moving Ownership content_markdown: > - When an owner is passed as an argument to a function, ownership is moved to - the function parameter. + When an owner is passed as an argument to a function, ownership of the + value is moved to the function parameter. This means that the original + variable in the calling function loses access to the value and can no + longer be used. - After a **move** the variable in the original function can no longer be - used. + After a move, the original variable becomes invalid, and attempting + to use it will result in a compiler error. This ensures memory safety + by preventing use-after-move issues. Memory details: * During a **move** the stack memory of the owners value is copied to the - function call's parameter stack memory. + function call's parameter stack memory. For heap-allocated data (e.g., owned + types like String or Vec), the pointer to the heap memory is moved, but the + actual data on the heap is not copied. Instead, the ownership of the heap + memory is transferred. + + + When values are dropped: + * A value is dropped (cleaned up) when it goes out of scope. For function + parameters, this occurs at the end of the function's execution. + code: >- https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=struct%20Foo%20%7B%0A%20%20%20%20x%3A%20i32%2C%0A%7D%0A%0Afn%20do_something(f%3A%20Foo)%20%7B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20f.x)%3B%0A%20%20%20%20%2F%2F%20f%20is%20dropped%20here%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20foo%20%3D%20Foo%20%7B%20x%3A%2042%20%7D%3B%0A%20%20%20%20%2F%2F%20foo%20is%20moved%20to%20do_something%0A%20%20%20%20do_something(foo)%3B%0A%20%20%20%20%2F%2F%20foo%20can%20no%20longer%20be%20used%0A%7D%0A - title: Returning Ownership