|
|
|
|
|
by bct
4685 days ago
|
|
I'm new to Rust and I'm not familiar with this code, but here's an attempt at an explanation anyways: The "error" function returns a struct containing the error string. I suspect the intention is to allow dynamically constructed error messages like "syntax error at line 4, char 3" (although it doesn't seem to be doing this anywhere). Since the string might be dynamically allocated, it has to be freed when the struct is freed, which obviously can't happen if the struct just has a pointer to the literal in static memory. (You could flag the string somehow as "should be freed/should not be freed", as you suggest in your other comment, but this has its own tradeoffs) If you wanted an "error" function that would take string literals directly (and only string literals) you would do something like this: https://gist.github.com/bct/6300740 The "parse_ident" function doesn't return any portion of the input string, so it can just use a borrowed reference. |
|
And how about some convenient "either or"?