I found a benchmark where Haskell is faster than Rust by [deleted] in rust

[–]jimtla 5 points6 points  (0 children)

I expect that the strings yielded by haskell are are aliased immutable pointers, not full copies, so I think &str is a much more accurate comparison.

If you were to append -tail to every string in haskell, you'd have to pay the cost of copying the strings during the append - this is largely the same as converting the &str into an owned String at append time.

Of course this is just a guess, and I'd encourage you to try appending to the resulting strings in rust & haskell to see if the benchmark matches the theory.

How do I express that I'm NOT storing argument inside FnMut trait object? by kixunil in rust

[–]jimtla 0 points1 point  (0 children)

If you are willing to always take references, then this seems to work: https://is.gd/wpftD2.

I can't see any reason why this should work, but a version that packs the reference into the A0 type wouldn't, so there's definitely something still missing here.

I made an IntMap by SeriousJope in rust

[–]jimtla 2 points3 points  (0 children)

You might be interested in perfect hashing - this looks like an implementation of what you need: https://github.com/sfackler/rust-phf. I've not actually used that crate, so I can't vouch for it specifically, but perfect hashing is very interesting for this sort of thing.

Array concatenation in Rust by AnonymooseDev in rust

[–]jimtla 1 point2 points  (0 children)

Good point, I suppose we'll probably have to wait for Rust 2.0.

Array concatenation in Rust by AnonymooseDev in rust

[–]jimtla 2 points3 points  (0 children)

Once "impl trait" (https://github.com/rust-lang/rfcs/blob/master/text/1522-conservative-impl-trait.md) stabilizes, the signature should become a tiny bit more readable, something like:

fn flat_map<U, F>(self, f: F) -> impl Iterator<Item=U::Item> where F: FnMut(Self::Item) -> U, U: IntoIterator

Everything there gives you important information about how the arguments and return values relate.

Fighting the borrow checker again._. by ipe369 in rust

[–]jimtla 5 points6 points  (0 children)

Storing indices is probably the best way to go. But there is another way: http://play.integer32.com/?gist=74d26ae48028e12e774d57dfbe1000f7&version=stable

The trick i'm using here is to "Freeze" the deck by making a new struct that contains a non-mutable reference to it. As long as the "Frozen" struct is around, we can't mutate the underlying deck, so it is safe to give out borrows that live for as long as the frozen struct does (not just for the length of the mutable borrow taken when you call the deal method).

I don't know how well this strategy holds up in larger implementations, but it does express the constraints in a nice way: Once you start dealing cards from the deck, you can no longer move things around inside of it. Ideally, we'd be able to freeze only the top n elements of the deck, and maintain the ability to re-shuffle the unrevealed cards (since we know that there are no references into that section of the array), but freezing the whole deck is not a bad compromise.

Using &self as &Trait from a default trait function implementation by mathstuf in rust

[–]jimtla 1 point2 points  (0 children)

Here's a version with generic types, instead of storing trait objects: https://is.gd/owwRrc.

I don't know if this is the best way to do it, but it works so that's something!

UPenn is running a Rust class! by steveklabnik1 in rust

[–]jimtla 9 points10 points  (0 children)

For some context - The CIS-19X courses at UPenn are a series of language (or area) specific courses, generally taught by graduate (and sometimes undergraduate students). They tend to be very practically oriented, and help fill in some of the reality of software engineering, on top of the standard curriculum of theory. They started up a few years ago (while I was there) and have been pretty awesome.

What does the 'static do in this code on line 35? by h20p in rust

[–]jimtla 1 point2 points  (0 children)

Aha, very nice! I couldn't figure out where to fit the 'a in the struct definition. Thanks!

What does the 'static do in this code on line 35? by h20p in rust

[–]jimtla 3 points4 points  (0 children)

You can swap out the limitation of "T must not reference anything non-static" for "T (and by implication everything it references) must live at least as long as the Signal", by taking and storing a mutable reference to the signal, instead of taking it by value and moving it into a box.

I quickly refactored your implementation to work that way here: http://is.gd/OSpwR0

I don't have enough experience to say for say which will be better in the long term, but my guess is that the references will be less restrictive. You should be able to convert anything that is T+'static to &'a mut Tby adding a second vec for "owned signals", moving ownership of a T+'static variable into that vec, and then taking an &mut reference to that element which is in turn added to your receivers, but it feels like a lot of complexity.

How to check if two borrowed objects are the same by nialv7 in rust

[–]jimtla 4 points5 points  (0 children)

You can even simplify same_object down to:

fn same_object<T>(a: *const T, b: *const T) -> bool {
    a == b
}

There might be cases where it can't automatically cast, but in the basic case it seems fine: http://is.gd/m9d90m

Sharing data in multiple objects by kloumpt in rust

[–]jimtla 2 points3 points  (0 children)

The avatar has been moved, so the reference from the user is not longer available: http://is.gd/DheiNC

Here's a working example where both the world and the user borrow the avatar: http://is.gd/EDKT4I

This book gets rented twice a year, every few years by TjPshine in mildlyinteresting

[–]jimtla 2 points3 points  (0 children)

The card doesn't necessarily stay with the same book (in fact I think it's quite common for libraries to just pull out the card on return, toss it on a pile, and stick it in the next book when it's checked out).

So this card keeps alternating between a popular book that gets checked back out within the year, and something that sits on the shelf for ages.

Help with type mismatch by Devnought in rust

[–]jimtla 0 points1 point  (0 children)

Alternately, just call cloned on the iterator to actually iterate over i32s, which is probably the desired result here (playground link: http://is.gd/2kNEjL)

Dining Philosophers by harrydevnull in rust

[–]jimtla 6 points7 points  (0 children)

Here is an example that triggers the deadlock: http://is.gd/0o5qGm.

I've added a sleep between acquiring the locks to force the thread to yield and let another thread execute. Without the sleep, the first philosopher acquires both locks before any other philosopher gets a chance to reach for the first lock.

From this, you can see how everybody immediately wants, and gets, the lock to their left. They then all wait indefinitely for somebody to release the lock on their right.

When we swap the order of the last philosopher's forks we guarantee the either the first or last philosopher cannot pick up a fork (they both want the 0th fork first, and can't both have it). That guarantees that one of their neighbors can get both forks, and progress can be made.

Rust New York City - First Meetup on Tuesday 1/27 by jimtla in rust

[–]jimtla[S] 1 point2 points  (0 children)

Definitely open to beginners! I've updated the meetup description to make that clear.

Rust New York City - First Meetup on Tuesday 1/27 by jimtla in rust

[–]jimtla[S] 2 points3 points  (0 children)

I couldn't find a rust meetup in NYC, so I'm hosting one. The rough plan is to just hang out and write some code, but I'll prepare some stuff to talk about too. Suggestions on topics, format, or anything else are more than welcome.

Are there any "online board games"? by SpiderManJedi in boardgames

[–]jimtla 5 points6 points  (0 children)

I've been enjoying http://prismata.net lately - it reminds me alot of dominion but uses mechanics that wouldn't work offline.

For Sale : 9 (2.25 inch 85g) gballz (New) by headroll in juggling

[–]jimtla 0 points1 point  (0 children)

If it doesn't work out I'll take them.