Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change String representation to actual #1817

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/memory-management/review.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ fn main() {
```

```bob
Stack Heap
.- - - - - - - - - - - - - -. .- - - - - - - - - - - - - - - -.
: : : :
Stack
.- - - - - - - - - - - - - -. Heap
: : .- - - - - - - - - - - - - - - -.
: s1 : : :
: +-----------+-------+ : : +----+----+----+----+----+ :
: | ptr | o---+---+-----+-->| H | e | l | l | o | :
: +-----------+-------+ : : :
: | capacity | 5 | : : +----+----+----+----+----+ :
: | ptr | o-+---+-----+-->| H | e | l | l | o | :
: | len | 5 | : : +----+----+----+----+----+ :
: | capacity | 5 | : : :
: +-----------+-------+ : : :
: : `- - - - - - - - - - - - - - - -'
`- - - - - - - - - - - - - -'
: : : :
`- - - - - - - - - - - - - -' `- - - - - - - - - - - - - - - -'
```

<details>
Expand All @@ -65,8 +65,8 @@ fn main() {
// String provides no guarantees about its layout, so this could lead to
// undefined behavior.
unsafe {
let (ptr, capacity, len): (usize, usize, usize) = std::mem::transmute(s1);
println!("ptr = {ptr:#x}, len = {len}, capacity = {capacity}");
let (capacity, ptr, len): (usize, usize, usize) = std::mem::transmute(s1);
println!("capacity = {capacity}, ptr = {ptr:#x}, len = {len}");
Comment on lines +68 to +69
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be better to use [String::into_raw_parts](https://doc.rust-lang.org/std/string/struct.String.html#method.into_raw_parts) here? What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, thanks for making me aware of this, I'm just learning rust, so I didn't know about it.

It's cool, that the string library has a feature for this, and it's a cool alternative.

I wouldn't go for it for two reasons:

  • weak reason: it's still unstable feature, so you have to enable it, otherwise it's a compile error
  • stronger reason: the currently demonstrated method works with any other type that one knows the representation for, so it has a bit more of an educational value, and there is ample warning there to say that this is for debugging, not for production

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both good points!

}
}
```
Expand Down