Live data from Hacker News

How to not rewrite it in Rust

adventures.michaelfbryan.com

31–40 of 231 posts

Re: How to not rewrite it in Rust

#31
post #22

Earlier quoted context omitted.

Passing around pointers to objects on the stack. In another language you'd risk having a pointer to invalid memory when the function returns if the pointer escapes the function, and so it is advised to use pointers to objects on the stack sparingly if at all. In rust, the compiler will statically determine if a pointer to an object on the stack could escape the function, and if so will fail to compile.

> In another language you'd risk having a pointer to invalid memory when the function returns if the pointer escapes the function, and so it is advised to use pointers to objects on the stack sparingly if at all. Just a nitpick, this is only true for non-garbage-collected (or refcounted) languages. :-)

In garbage collected languages, are there any objects on the stack? I’m not familiar with GC implementations, but at least theoretically everything is allocated on the heap. Maybe some implementations keep things on the stack as an optimization?

Re: How to not rewrite it in Rust

#32
post #14

This article is wonderful and I hope as a setup for second article, "How to Rewrite it in Rust"! > However at best, the temptation to RiiR is unproductive > A much better alternative is to reuse the original library and just publish a safe interface to it. Just as models are a lower dimensional representation of a more complex problem, [1] I have to re-iterate that there are no truth(ism)s in software and as in all e…

I would like to add my 2 cents,

TBH I don't see a future where RIIR every piece of software would make sense, and I think you sort of put it in there somewhere in your comments.

But yes I do see that for many it would and for those, RIIR would IMHO be an incremental process of oxidizing your project to the point that there's nothing left but Rust.

Writing something from scratch and waiting for it to finish is the biggest problem we face, cause eventually the will to continue with the effort just dies. But creating a meaningful ground not just some "safer" bindings does help in inviting others to help share the effort as well.

So hopefully people will be smart and identify what they should do for their projects, should it be a rewrite or should it be a new feature that you write in Rust. :)

And no I don't think bindings are the solution to anything, they serve no purpose in Rust community as a long term measure, you shouldn't have to sacrifice safety and/or performance incase of high level language.

Re: How to not rewrite it in Rust

#33

Earlier quoted context omitted.

All of Rust's complexity for strings comes out of complexity due to UTF-8. Well, a tiny bit comes from the fact that Rust has pointers too, but most of it is UTF-8. Did you happen to read the book? We cover strings early because this is a common pain point. Also, if you have something more detailed than "get strings out of libraries", I can give better advice. It's tough to tell what the actual issue is.

Like this: use sha2::{Sha256, Digest}; fn main() { let mut hasher = Sha256::new(); hasher.input(b"hello world"); let result = hasher.result(); ?? } How do I get a string with the hash value? Which chapter of which book should I read?

Ah, I see! So yeah, this isn't something the book covers, so that would not be helpful.

In this case, the hash ends up being raw bytes. So the question is, what are you trying to do with this hash? Being a bunch of bytes, this may not be valid UTF-8 (and in this case, is not ASCII, let alone UTF-8). One option is to encode to base64:

  use sha2::{Sha256, Digest};
  use base64;
  
  fn main() {
      let mut hasher = Sha256::new();
      hasher.input(b"hello world");
      let result = hasher.result();
      let encoded = base64::encode(&result);
    
      assert_eq!("uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=", encoded);
  }
But yeah, you can't exactly "get a string" out of a random bag of bytes unless you know how you want that bag of bytes to be represented, encoding wise. That's not exactly a satisfying answer, but such are strings!

Re: How to not rewrite it in Rust

#34
Oh no, it's not available, maybe you could rewrite your website in rust to handle the load? ;)

...this is tongue-in-cheek comment. I do agree with the author, "rust rewrite" alternative of wrapping c library is way better in many cases for well established or legacy libs. However it depends on the momentum you can bring - as pure rust implementation for widely used areas can benefit from compiler grinding through it. I can see rust written low level libraries used across all high-level languages in the near future.

Re: How to not rewrite it in Rust

#35
post #29

Earlier quoted context omitted.

> so this sounds like it's describing a bad way to rewrite something in Rust, which is not the case. I think this kind of title is trying to do exactly that, or at least, that's how I took it. > It also isn't the wording used in the original article title. This is a good reason to switch it, for sure. I didn't realize this, if you had led with that, I wouldn't have said anything :)

> I think this kind of title is trying to do exactly that, or at least, that's how I took it I took the exact opposite: This article is describing an alternative to RiiR, so that RiiR is avoided when not needed.

I don't disagree with you is that the thesis of the article is "don't re-write something in Rust."

What I was trying to say is that I think the title is making a joke; it's saying "how to re-write something in Rust badly", because well, it's not re-writing it in Rust at all.

Re: How to not rewrite it in Rust

#36

Earlier quoted context omitted.

All of Rust's complexity for strings comes out of complexity due to UTF-8. Well, a tiny bit comes from the fact that Rust has pointers too, but most of it is UTF-8. Did you happen to read the book? We cover strings early because this is a common pain point. Also, if you have something more detailed than "get strings out of libraries", I can give better advice. It's tough to tell what the actual issue is.

Like this: use sha2::{Sha256, Digest}; fn main() { let mut hasher = Sha256::new(); hasher.input(b"hello world"); let result = hasher.result(); ?? } How do I get a string with the hash value? Which chapter of which book should I read?

As mentioned by others, you need to convert the bytes to a String. `Sha2` provides a helper function for that: `hasher.result_str()`.

EDIT: sorry I don't know which docs I was looking at, I can't find it in the current version.

Re: How to not rewrite it in Rust

#37
post #30

Earlier quoted context omitted.

All of Rust's complexity for strings comes out of complexity due to UTF-8. Well, a tiny bit comes from the fact that Rust has pointers too, but most of it is UTF-8. Did you happen to read the book? We cover strings early because this is a common pain point. Also, if you have something more detailed than "get strings out of libraries", I can give better advice. It's tough to tell what the actual issue is.

Which book? I'm just starting the O'Reilly book, coming from Python

"the book" colloquially means https://doc.rust-lang.org/stable/book/ (I'm one of the authors)

The O'Reilly book is good too.

Re: How to not rewrite it in Rust

#38
post #22

Earlier quoted context omitted.

Passing around pointers to objects on the stack. In another language you'd risk having a pointer to invalid memory when the function returns if the pointer escapes the function, and so it is advised to use pointers to objects on the stack sparingly if at all. In rust, the compiler will statically determine if a pointer to an object on the stack could escape the function, and if so will fail to compile.

> In another language you'd risk having a pointer to invalid memory when the function returns if the pointer escapes the function, and so it is advised to use pointers to objects on the stack sparingly if at all. Just a nitpick, this is only true for non-garbage-collected (or refcounted) languages. :-)

You omitted an important part: the pointer pointing to the stack. In GC'd or refcounted languages, (almost) everything is on the heap (occasional exceptions being primitive types like integers). This of course leads to worse performance because of an additional dereferencing step and cache misses.

Re: How to not rewrite it in Rust

#39
post #16

Earlier quoted context omitted.

That's not true. If you build from a dist tarball, you do not need auto tools installed. You usually do if you build from a git repo, however.

How/why is a dist tarball not exactly the same as a zipped up git tree?

The parent you're replying to answers that question. And if your question is meant rhetorically, it doesn't contribute to the discussion either.

Re: How to not rewrite it in Rust

#40

> Rust gives you the power to do things which would be unheard of (or just plain dangerous) in other languages What are examples of the unheard of things?

Passing around pointers to objects on the stack. In another language you'd risk having a pointer to invalid memory when the function returns if the pointer escapes the function, and so it is advised to use pointers to objects on the stack sparingly if at all. In rust, the compiler will statically determine if a pointer to an object on the stack could escape the function, and if so will fail to compile.

>so it is advised to use pointers to objects on the stack sparingly

At least in the C/C++ world, this is not true. Where possible, we prefer stack allocated objects. Stack allocation is fast and usually gives you better cache performance than heap allocation.

Rust's lifetime/borrow checker provides compiler support for well-established best practices amongst professional C and C++ users.

Post reply on HN