Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Rothon posted:

Rust By Example is in a similar style to the Go tour. There's also the Rust Book which a more traditional format.

I'm kind of offended by hello world being implemented with a macro.

Adbot
ADBOT LOVES YOU

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Rothon posted:

It allows format arguments to be verified by the compiler. You could write the string literal directly to the stdout writer if you wanted to, but it's much more common to just use println!. It's basically equivalent to doing
code:
#include <stdio.h>

int main() {
    printf("hello, world!\n");
}
in C, except that compilation will fail if you accidentally put a format specifier in the string instead of maybe warning and reading some trash off the stack.

I mean, I guess it comes down to whether a hello world implementation is meant to demonstrate idiomatic code or whether it's meant to give insight into how the language operates. The macro completely obfuscates whatever is going on there and as far as I know, it could be hiding java levels of boilerplate (which would be fine or even cool).

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
I did some rust this weekend and I will feel pretty convinced that it's going to be a big deal.

I know you were all anxious to get my opinion so there it is.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Ralith posted:

The author of error-chain has posted a nice introduction to the newest release which reveals that my use of foreign_links above was somewhat less than idiomatic and makes a better case for why it's worth using beyond saving boilerplate.

Oh wow, this is so cool.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Ralith posted:

You can chain_err any Result type if you have the symbols from a module containing an error_chain! invocation in scope. The method comes from a macro-generated ResultExt trait, and it returns the type of Result defined by that macro. For simple command-line tools, I often just have "error_chain! {}" at the top of the file so I can provide nice detailed information-preserving textual error messages everywhere.

Do you have any good examples of command line tools written in rust?

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Ralith posted:

The tools I've been writing recently wouldn't make much sense to share, but I hear ripgrep is pretty neat. If you're looking for examples of error-chain use specifically, the best I can do is suggest you page through the reverse dependency list. Maybe Xargo?

Thanks for reminding me of rip grep. I'd been meaning to check out this code review of ripgrep, and it is indeed pretty fantastic for language learners. (Thanks for the other links too, I'll check them out).

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
So, I want to parse a protobuf result and then match over the possible return types, but I've got no idea how to to do it.

Protobuf types:

code:
pub fn parse_from_bytes<M: Message + MessageStatic>(bytes: &[u8]) -> ProtobufResult<M>
type ProtobufResult<T> = Result<T, ProtobufError>;
What I'm attempting to do:
code:
mod dispatch {
    use protobufs; // my compiled protobufs
    use protobuf;
    use task;

    pub fn dispatch_msg(m: &[u8]) -> Result<task::Result, &'static str> {
        // match protobuf::parse_from_bytes(m).unwrap() {
        //
        // }
        // Err:
        // match protobuf::parse_from_bytes(m).unwrap() {
        //       ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for `_`
        
	// works
	let pb: protobufs::task::enq = protobuf::parse_from_bytes(m).unwrap();
        println!("{:?}", pb);
        return Ok(task::Result {});
    }
So, basically, I want to parse the protobuf message, and then match over the result, performing a dispatch depending on the type. But I'm clearly going about it the wrong way. Any clues?

I understand that it cant `parse_from_bytes` without a concrete type to return. I think I'm just using the protobuf library wrong but it has very little documentation so I'm just trying to follow the compiler to victory.

DONT THREAD ON ME fucked around with this message at 23:05 on Dec 18, 2016

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Ralith posted:

You can explicitly specify type parameters by calling a function with syntax like "parse_from_bytes::<MyMessageType>(m)". You may also get better results from inference once you've actually entered some cases that entail a specific concrete discriminant type.

Alternatively, you could switch to cap'n proto!

I assume you're aware that in real code you shouldn't call unwrap like that, and should instead define an error type which can clearly express/embed the cause of a failure, for example using error-chain.

boo! I tried that but I was doing parse_from_bytes<MyMessageType>(m). The parse errors probably should have clued me in.

And yeah, I'm still getting used to error handling, but I recognize that by just calling unwrap, I'm failing to handle errors.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
Yeah, refactoring can be super annoying, especially if you're adding or removing a lifetime or something. Fortunately, there's nothing stopping rust from having very powerful tooling, so I think it's just a matter of time.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Asymmetrikon posted:

I think my least favorite thing about refactoring so far has been trying to isolate parts of a chain of iterators. The result of a .map().filter().whatever() has a gnarly result type.

There's also inspect: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.inspect

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
So, I'm at a point with rust where I am comfortable with all of the syntax, but am still struggling with borrowing, lifetimes and references. This tutorial is tremendously helpful:

http://cglab.ca/~abeinges/blah/too-many-lists/book/README.html

It walks you through building a number of linked lists, gradually increasing in complexity. It specifically addresses things like Box, Rc, Arc, mem::Replace, &mut, *mut, *const, Copy, and how you'll need to use those things to build a simple data structure in Rust. It's really helped me.

The format is a little annoying (probably too jokey for most people's taste) but the content is solid.

DONT THREAD ON ME fucked around with this message at 19:01 on Jan 7, 2017

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
I really like rust and I can't wait for it to become employable so I can get a job writing it.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
I'm playing with Rocket and Diesel -- they're the first real frameworks I've tried in rust (tokio is probably the largest library I've dealt with).

CodeGen and Macros are going to ruin this language.

http://docs.diesel.rs/diesel/macro.infer_table_from_schema.html

I'm flabbergasted that the compiler even makes this possible. I'm doubly flabbergasted that Diesel presents this in their tutorial as the way to generate their schema.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Sagacity posted:

I think this is a pretty bad idea too but, to be fair, languages like Java do a ton of this stuff at compile time too, e.g. Jooq. What I dislike most about macro based DSLs is that inevitably they will end up like C++-style template monstrosities that are poorly documented and where there is no hope of sane autocompletion.

What concerns me is that rust made this incredibly easy. Yes, I was importing a crate called `_codegen`, which should have tipped me off, but beyond that it's just a macro. If this were enabled as part of a plugin, it would be a different story. Macros should not be able to do this, and now I'm going to have to be incredibly suspicious of every single macro I encounter because it could be doing network insanity at compile time. (I could be mistaken about some of this, I haven't actually figured out how this macro works yet).

DONT THREAD ON ME fucked around with this message at 19:59 on Aug 12, 2017

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
Weird, it never occurred to me that you could use custom derive like that.

I assume procedural macros will allow similar behavior? If so I really hope they gate them somehow because I really don't want things like this to be standard macro behavior. But to Ralith's point, it's not like I ever really use a macro without understanding what it does, so maybe it's okay.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Ralith posted:

Procedural macros should generally be the exception, at least once declarative macros 2.0 lands. Hell, any kind of macros at all should be the rare exception, much less complicated ones. It's good to have escape hatches in a language, even if some people will always be tempted to treat the escape hatch more like a revolving door.

I think I've misunderstood the state of macros. My understanding was the procedurals macros were macros 2.0, but it sounds like you're saying that's not the case. Guess I have some reading to do.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
Yeah the intellij plugin is much better right now. RLS will likely be the superior choice eventually but it’s not there yet.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Beamed posted:

VS code shows me compiler/syntax errors immediately,
??? this definitely works for me. Maybe RLS + VSCode is better but I've never had it not just continually crash on me so I'm not sure what it's like.





quote:

lets me ctrl+click into library code to read some APIs,
cmd+b?

quote:

in-built terminal
intellij has this feature, I'm not sure how to use it because I prefer to use a stand aside terminal

i have no doubt that RLS will be the way to go eventually but it wasn't there last time I tried it (admittedly, around 6 months ago, haven't been doing much rust lately).

DONT THREAD ON ME fucked around with this message at 02:50 on Feb 14, 2018

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Beamed posted:

Nope, don't get this.


FAKEDIT: By the time I finished adding that to imgur and writing this post, it finally showed a red line. That.. I dunno, took awhile to recognize when I added the semicolon too. Doesn't spot the type error, either way.



Didn't say that IntelliJ didn't have these features, just meant to say VSCode supports the most necessary things pretty quickly.

REALEDIT: Perhaps ironically, I feel like IntelliJ will be the way to go in the future; the more fully fleshed out features it provides will be better than I think RLS will support.

That's weird friend, I dunno. Works on my machine, I guess!

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
I'm implementing Raft (for learning) and I'm using async/await futures 0.3 in compatibility with tokio 0.1.

It's still a pain in the rear end (mostly due to issues with the compat shims at this point) but async/await is way nicer to work with than chaining 0.1 futures. I'm actually getting stuff done instead of spending hours trying to understand why my types aren't lining up. It's going to be good eventually.

Adbot
ADBOT LOVES YOU

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

1. Yeah, I think you should absolutely stick with ws-rs for exactly the reasons you mentioned. Or maybe write your own websockets implementation. If you want to get more serious I'd consider tokio, which will handle (2) for you.

2. You probably want the nomicon, https://doc.rust-lang.org/nomicon/ IMO rust docs do a good job of educating people about the happy path but I've had a really hard time learning the lower level/unsafe details (this is definitely partly due to my own inexperience with lower level languages, rust was my first).

3. RLS is the biggest disaster in Rust ATM. I have faith that it will get there eventually but it's just not usable and it should not be recommended to new users. Intellij rust plugin is way way way way way way, way way better.

DONT THREAD ON ME fucked around with this message at 17:47 on May 31, 2019

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply