677 views|7 replies

4764

Posts

12

Resources
The OP
 

"Rust in Action" Ownership is something you really need to understand [Copy link]

I read it very slowly, and the ownership here is indeed not very similar to Python.

This is my first time to come across this kind of thinking, and it is indeed very interesting. But I also don’t understand it.

  1. Every value in Rust is owned by a variable, which is called the value's owner.
  2. A value can only be owned by one variable at a time, or a value can only have one owner.
  3. When the owner (variable) leaves the scope, the value is discarded (dropped)

I really can't figure it out. The following paragraph doesn't work

    let s1 = String::from("hello");
    let s2 = s1;

    println!("{}, world!", s1);

So why can I run this section?

    let s0: &str = "Hello";
    let s1 = s0;

    println!("{}, world!", s1);

This post is from Programming Basics

Latest reply

Because the latter is a reference, it should actually be &'static static reference, pointing to the static data area. You can also think that continuous move also points to the same area, so it is OK. The previous compilation will fail. You can think of it as the data on the stack. Move is taken away, which means the ownership is lost. For example, a heap new data cannot be referenced by multiple people, so there is a problem of multi-thread competition.  Details Published on 2024-9-9 18:09
 

4764

Posts

12

Resources
2
 

Add a screenshot of the code

Smart error reporting

But the second one is right

This post is from Programming Basics
 
 
 

1110

Posts

1

Resources
3
 

After understanding it, let s2 = s1; the ownership of s1 is transferred to s2, and s1 is gone with the wind.

If you print a disappeared s1 again, an error will be reported.

The second one let s1 = s0. Since s0 is a reference, no error will be reported because everyone is borrowing it.

This post is from Programming Basics
 
 
 

1110

Posts

1

Resources
4
 

The question is:

let s1 = String::from("Hello")

and

let s2 = "Hello"

It's the difference

It doesn't make sense, it's designed that way, s1 is an object allocated on the heap, s2 is a reference to a temporary object (are you sure?)

This post is from Programming Basics

Comments

My superficial understanding is that s1 is of the String composite type (), s2 is of the &str type, and the & address operator is interesting. It means that Rust hard-codes "Hello" into the code at compile time, an immutable string (01010101...) and then let s2 = "Hel  Details Published on 2024-4-23 14:31
 
 
 

7422

Posts

2

Resources
5
 

It is said that doubly linked lists are difficult to write...

This post is from Programming Basics

Comments

Yeah, but for me, I can’t write a linked list in any language, hahaha.  Details Published on 2024-4-23 14:31
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

4764

Posts

12

Resources
6
 
hellokitty_bean posted on 2024-4-23 09:46 The question is: let s1 = String::from("Hello") and let s2 = "Hello" are...

My superficial understanding is that s1 is a String composite type (),

s2 is of type &str. The & address operator is quite interesting.

This means that Rust hard-codes "Hello" into the code at compile time, an immutable string (01010101...)

Then when let s2 = "Hello", it will find a space for you to copy that string into it.

The same is true when we let s3 = s2, because s2 is that string, so we still play it this way.

This post is from Programming Basics
 
 
 

4764

Posts

12

Resources
7
 
freebsder posted on 2024-4-23 11:46 It is said that doubly linked lists are difficult to write. . .

Yeah, but for me, I can’t write a linked list in any language, hahaha.

This post is from Programming Basics
 
 
 

6

Posts

0

Resources
8
 
Because the latter is a reference, it should actually be &'static static reference, pointing to the static data area. You can also think that continuous move also points to the same area, so it is OK. The previous compilation will fail. You can think of it as the data on the stack. Move is taken away, which means the ownership is lost. For example, a heap new data cannot be referenced by multiple people, so there is a problem of multi-thread competition.
This post is from Programming Basics
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list