Live data from Hacker News

How to not rewrite it in Rust

adventures.michaelfbryan.com

61–70 of 231 posts

Re: How to not rewrite it in Rust

#61
post #32

Earlier quoted context omitted.

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 big…

> TBH I don't see a future where RIIR every piece of software would make sense It doesn't necessarily have to be Rust, and it's a process that may take decades. But I really do think that absolutely everything should be rewritten in a memory-safe language (i.e. not C/C++). The vast majority of security issues are either stupid misconfigurations or memory-safety issues. And currently most security initiatives are unde…

Exactly, when we talk about RiiR, we are saying, "I would like to have the guarantees that Rust provides". If the Rust ecosystem depends heavily on C/C++ libraries, then the system has the properties of the union of all the flaws and the properties of the language do not translate into a quality of the ecosystem.

That said, I think containing C/C++ to a Wasm sandbox that can be integrated transparently with Rust would be a gigantic win for security, correctness and the Rust ecosystem.

Re: How to not rewrite it in Rust

#62

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?

since we know that Sha256::new() will always produce 'valid' characters for utf8 you can just do

    let my_rust_string = str::from_utf8(result).unwarp() 

If there is a possibility of an error, (you read it from file, network ...) you can do:

    let my_rust_string = match str::from_utf8(result) {
        Ok(str) => str,
        Err(err) => panic!("Invalid utf8: {}", err),
    };
 
Or you can do lossy conversion (will have that question mark on chars that it cant decode)

    let my_rust_string = String::from_utf8_lossy(result);

edit: Ok so i read sha2 api wrong. result() is raw bytes and result_str() is hex encoded string. Above wont be much use for either. And others have already explained what to use.

I just stared programming in rust several months ago, and that was my pain also, since where i live we still have some other encoding in use.

Re: How to not rewrite it in Rust

#63

> The first step in interfacing with a native library is to understand how it was originally intended to work. But reading code is hard, it's easier to project your sense of confusion onto your predecessor than own it yourself, and once you rewrite the code in Rust and start to understand the true complexity that the previous code had to work around, you'll leave for another job (this time with Rust on your resume).

This is a very negative comment. Except I have seen it happen many times.

Re: How to not rewrite it in Rust

#64
post #26
post #22

Earlier quoted context omitted.

> 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. :-)

Well, if you're gonna nitpick... A garbage-collected language wouldn't let you allocate such items on the stack in the first place. GC languages tend not to let you choose where to allocate in the first place, and have obligatory heap semantics for reference types. Some compilers (including Go and Java, to my knowledge) will attempt to optimize the implementation to stack allocation when possible using escape analysi…

Here goes a list from GC enabled languages that let you chose where to allocate.

Modula-3, Oberon, Oberon-2, Active Oberon, Mesa/Cedar, Component Pascal, Eiffel, Sing#, System C#, C#, Swift, D, Nim

Re: How to not rewrite it in Rust

#65
post #31
post #22

Earlier quoted context omitted.

> 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?

Yes, see my reply to other thread.

https://news.ycombinator.com/item?id=21335506

Re: How to not rewrite it in Rust

#67
post #45
post #2

The title should be "how to not" rather than "how not to".

Not really. "How not to" is a common idiom (like "X considered harmful" for such advice articles (regardless of correctness).

The TFA is literally "How to not" as in "how to avoid" not "How not to" which would be "Here's the wrong way to do it"

Re: How to not rewrite it in Rust

#68

> The first step in interfacing with a native library is to understand how it was originally intended to work. But reading code is hard, it's easier to project your sense of confusion onto your predecessor than own it yourself, and once you rewrite the code in Rust and start to understand the true complexity that the previous code had to work around, you'll leave for another job (this time with Rust on your resume).

It's called resume driven development, and the reason it's done is because it helps those who do it get jobs

Re: How to not rewrite it in Rust

#69

> The first step in interfacing with a native library is to understand how it was originally intended to work. But reading code is hard, it's easier to project your sense of confusion onto your predecessor than own it yourself, and once you rewrite the code in Rust and start to understand the true complexity that the previous code had to work around, you'll leave for another job (this time with Rust on your resume).

My life as an immigrant developer, therefore cheaper, hired to fix the shit of the elite quitters :D

Sometime I catch them a few weeks before they quit and I get to ask why they developed a low level http server in java interpreting controller code written in javascript that nobody understand but them. "It's much more efficient than using php or nodejs or higher level java" "you benchmarked ?" "I have to go do more knowledge transfer, bye"...

I dont know what I'd do if they were allowed by the company to do Go or Rust :D Guess I'd redo their wheel reinvention in the majority language of the team... increasing the overall constant migration I see again and again...

Re: How to not rewrite it in Rust

#70
post #29

Earlier quoted context omitted.

> 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.

I thought the title was making a joke, but then I read the article and IMO the actual article title of "How to not RiiR" is much more indicative of it's content than the HN title of "How Not to RiiR"
Post reply on HN