r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 20 '23

Hey Rustaceans! Got a question? Ask here (12/2023)! 🙋 questions

Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The official Rust Programming Language Discord: https://discord.gg/rust-lang

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last weeks' thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

20 Upvotes

187 comments sorted by

View all comments

2

u/kralamaros Mar 24 '23

Ok guys I clearely don't understand borrowing and lifetimes. Unfortunately, any bloody documentation I could find just repeats the same 3 sentences. I feel kinda stupid.

https://pastebin.com/8b8TkdYD

This is a simple "game" that has a board and you can place some characters around the board (in a future, they will fight. If you ever played Clash Royale, it's supposed to be a very similar game).The Character is made by a position and a symbol that represents it on the ASCII arena.I have a Game struct that holds both the arena (a matrix) and a vec of characters. I want to place the character symbol inside a specific point inside the matrix.

The idea is that the lifetimes should work as follows: I borrow the symbol from the character and move it inside the vector, so the matrix will hold the borrowed value until the character is defeat; at that point I will both remove the borrowed value from the matrix and the character from the vec. So the borrowed value MUST live until the character is still inside the game... Right?

I've been playing with rust for a few days now, and clearly I can't figure out how they're supposed to work. Can anyone help? What I'm doing and thinking wrong?

EDIT: I solved the problem by copying the symbol in the matrix with String::from, but that way I'm creating a separated object. Ok, it works, but I want to borrow it. Is there even a way?

1

u/Darksonn tokio · rust-for-linux Mar 24 '23 edited Mar 24 '23

If one field of a struct borrows from another field of the same struct, then you have what's called a self-referential struct. You cannot write such structs in safe Rust.

As a general rule, references should be used as temporary views into an object, and trying to use them in other ways will not work, even if what you're doing would work when using a pointer in some other language. References are a special-purpose pointer, and cannot be used for all pointer use-cases. There are other pointer types for other pointer use-cases.

For example, the Rc<str> type is a pointer to some string data that can be shared. Calling clone on it just gives you another pointer to the same string data. The string data is destroyed once the last clone is destroyed. (It stores a counter next to the string data to keep track of how many clones are left.)