These are chat archives for rust-lang/rust

20th
Nov 2017
Bernhard Schuster
@drahnr
Nov 20 2017 10:44
@stern0 Mutex usually encapsulates the object to protect, doesn't it?
Ilya Bogdanov
@vitvakatu
Nov 20 2017 10:46
@stern0 it's not quite clear what you're trying to achieve. If you want to protect your packet struct, you need to use Mutex<packet>. So usual pattern here would be smth like struct ProtectedPacket(Mutex<packet>)
cllty
@cllty
Nov 20 2017 11:34
@drahnr yes kind of
@vitvakatu thanks what about sender and receiver channels??
is it possible to pass a struct to a channel??
Bernhard Schuster
@drahnr
Nov 20 2017 11:36
yes
can you please provide some context of what you are trying to achieve
cllty
@cllty
Nov 20 2017 11:37
I am writing a udp packet buffer
Ilya Bogdanov
@vitvakatu
Nov 20 2017 11:41
What are you trying to do with channels?
Bernhard Schuster
@drahnr
Nov 20 2017 11:42
that's golang...
can you put in words what you are trying to achieve with the channels?
cllty
@cllty
Nov 20 2017 11:42
that is in golang but would like to acheive same in rust
the receiver should be able to get data from the sender in some time in future
cllty
@cllty
Nov 20 2017 12:09
basically I want pass data from one thread to the other
Neverminder
@Neverminder
Nov 20 2017 12:21

Hi. I'm struggling with parameter type annotations. I'm sure it's something trivial, but just can't work out.

for row in &result {
    let el: String = row.get(0);
    println!("{:?}", el);
    println!("{:?}", row.get(0));
}

so

let el: String = row.get(0);
println!("{:?}", el);

works, but when I try to do it in one line it doesn't:

println!("{:?}", row.get(0));
error[E0283]: type annotations required: cannot resolve `_: postgres::types::FromSql`
  --> src/main.rs:31:24
   |
31 |         println!("{:?}", row.get(0));
   |                              ^^^

Any idea what's missing here?

Steve Klabnik
@steveklabnik
Nov 20 2017 12:21
well, it needs to know the type
you can't really annotate the type in just println
so the let is the righ tway to do it, imho
Neverminder
@Neverminder
Nov 20 2017 12:22
@steveklabnik so the second "inline" approach is not even supposed to work then?
Steve Klabnik
@steveklabnik
Nov 20 2017 12:23
@stern0 that's just what channels do, you don't need extra stuff
@Neverminder which by "second"? the ones that work should work, and the ones that don't shouldn't
Jonas Platte
@jplatte
Nov 20 2017 12:24
@Neverminder Assuming the get in that example is this one, println!("{:?}", row.get::<_, String>(0)); should work too.
Steve Klabnik
@steveklabnik
Nov 20 2017 12:24
ah yeah depending on the signature of get that might work
Neverminder
@Neverminder
Nov 20 2017 12:25
thanks fellas, this is exactly what I was looking for
ISU
@isu17
Nov 20 2017 12:33
Hey everyone!
Jonas Platte
@jplatte
Nov 20 2017 12:34
@isu17 o/
Ritiek Malhotra
@ritiek
Nov 20 2017 12:40
How can I use MIR
Steve Klabnik
@steveklabnik
Nov 20 2017 12:41
@Ritiek to do what?
Ritiek Malhotra
@ritiek
Nov 20 2017 12:41
How can I use MIR borrowck instead of AST in rustc? I am new, sorry.
Steve Klabnik
@steveklabnik
Nov 20 2017 12:42
it's all good!
let me look it up
Ritiek Malhotra
@ritiek
Nov 20 2017 12:42
okie
Steve Klabnik
@steveklabnik
Nov 20 2017 12:44
-Znll
so like
if you're using cargo, cargo +nightly rustc -- -Z unstable-options -Znll
i believe
Ritiek Malhotra
@ritiek
Nov 20 2017 12:47
thanks! i'll let you know after i try it out
yep, that is it
Steve Klabnik
@steveklabnik
Nov 20 2017 12:51
:0
:)
ISU
@isu17
Nov 20 2017 12:53
hey im new to Rust, im coming from C, JS and Python, I know it looks a lot like C but its not, especially the pointers etc., right? Any tips for newcomers? I saw that there is no ternary operator and the loops are kinda different than what I am used to. Is there actually functional style of programming in Rust?
Steve Klabnik
@steveklabnik
Nov 20 2017 12:53
there's no ternary because if is an expression, so let x = if true { 5 } else { 6 }; Just Works
rust can feel functional at times
i am biased, by my tip is "read the book" (I'm the co-author :wink: )
or Rust By Example
ISU
@isu17
Nov 20 2017 12:55
Oh, nice
Bernhard Schuster
@drahnr
Nov 20 2017 12:56
What is the best way to access elements of a treewise datastructure
Any tips?
ISU
@isu17
Nov 20 2017 12:57
im reading rust by example right now
Bernhard Schuster
@drahnr
Nov 20 2017 12:57
It's a Vec<Action> where enum Action has a member with Action::Loop(Vec<Action>)
Any suggestions?
I considered a sequence of Vec<offset> to determine the element ( current active Vec<Action> then within that go another offset, and within that another, until it is either a non Loop Action or the offset Vec is traversed) , but that seems really really nasty I am not keen on going down that road
Jonas Platte
@jplatte
Nov 20 2017 13:03
@drahnr So you want to be able to refer to the position of an element within a tree?
Bernhard Schuster
@drahnr
Nov 20 2017 13:03
pretty much
Jonas Platte
@jplatte
Nov 20 2017 13:03
Why?
Bernhard Schuster
@drahnr
Nov 20 2017 13:04
because I want to do something with those actions in sequence and I'd like to do it in an iterator like way (not impl iter but similiar)
Jonas Platte
@jplatte
Nov 20 2017 13:07
@drahnr And references wouldn't work?
Bernhard Schuster
@drahnr
Nov 20 2017 13:09
How would I implement something likenext() then?
Jonas Platte
@jplatte
Nov 20 2017 13:12
I think you need to explain what you want to do in a bit more detail.
So basically all you want is an impl Iterator for MyRecursiveTypeIter?
Bernhard Schuster
@drahnr
Nov 20 2017 13:13
I do not need the full iteration spec, I need next() -> Self reset() and is_end() -> bool
Jonas Platte
@jplatte
Nov 20 2017 13:13
Do you want a mutable or immutable iterator over references, or an iterator that consumes your tree structure?
Bernhard Schuster
@drahnr
Nov 20 2017 13:14
no consumption
the context is a custom DSL for testing
Jonas Platte
@jplatte
Nov 20 2017 13:14
I don't see why you wouldn't use the standard Iterator
your next + is_end is exactly the same as Iterator::next, except yours would be harder to use
Bernhard Schuster
@drahnr
Nov 20 2017 13:15
well, you are right, I guess I could just make it work infinite repetitions too
Jonas Platte
@jplatte
Nov 20 2017 13:15
and reset is the same as just creating a new instance of the iterator type
Bernhard Schuster
@drahnr
Nov 20 2017 13:15
So in the end, I still need a state within the iterator
So the problem itself is still there, how to index :)
Jonas Platte
@jplatte
Nov 20 2017 13:16
let me try to write up a simple example...
I think I have an idea how to write this
Bernhard Schuster
@drahnr
Nov 20 2017 13:16
I guess my datastructure is just plain stupid
Jonas Platte
@jplatte
Nov 20 2017 13:17
Two actually..
Bernhard Schuster
@drahnr
Nov 20 2017 13:17
The one I described above
That would be awesome!
(even if it fails, let me know of how you tried it, so I don't run into the same dead end)
ISU
@isu17
Nov 20 2017 14:43
is rust used in micro controllers or someone ever tried to do so?
Steve Klabnik
@steveklabnik
Nov 20 2017 14:43
depends on what you mean by "micro" but yes, absolutely
32-bit is the low end right now, but 8 and 16 bit targets are working, but are still buggy and so not well integrated yet
Bernhard Schuster
@drahnr
Nov 20 2017 14:51
@ isu17 did you checkout https://github.com/helena-project/tock
ISU
@isu17
Nov 20 2017 14:57
oh, thank you
Jonas Platte
@jplatte
Nov 20 2017 15:01
@drahnr Okay, so I played around with my idea for a while and didn't manage to build something efficient (at least I'm pretty sure it's very inefficient...)
I think describing your current location in the tree and indexing with that would be more efficient
Bernhard Schuster
@drahnr
Nov 20 2017 15:03
You mean be specifiying the "left turns" to take?
And then do that nesting level by nesting level?
Jonas Platte
@jplatte
Nov 20 2017 15:03
But if you want to have a look anyway, here is my code, which basically just replicates the tree structure in the iterator type (referencing the data from the actual structure) and then consumes that iterator type: https://play.rust-lang.org/?gist=0e3579a703d6fb5ff020c46850801905&version=stable
It's relatively short, I guess that's a plus :D
Bernhard Schuster
@drahnr
Nov 20 2017 15:04
Thanks a lot anyways!
mnivoliez
@mnivoliez
Nov 20 2017 21:03
I got the borrow checker complaing at line 59 about self.tiles that can't be moved out of borrow content. Do you have any clue why it is winning? Thanks
Bradley Weston
@bweston92
Nov 20 2017 21:05
You have a full error message?
mnivoliez
@mnivoliez
Nov 20 2017 21:05
error[E0507]: cannot move out of borrowed content
  --> src/grid.rs:56:9
   |
56 |         self.tiles
   |         ^^^^ cannot move out of borrowed content

error: aborting due to previous error

error: Could not compile `a_star`.
I don't have much more
Denis Lisov
@tanriol
Nov 20 2017 21:07
Try changing the return type to &[Tile]
Ilya Bogdanov
@vitvakatu
Nov 20 2017 21:07
... or return self.tiles.clone()
mnivoliez
@mnivoliez
Nov 20 2017 21:08
Thanks, I'm an idiot... The error was line 56 and I was looking into the next function.....
no wonder why it was strange XD
anyway, what do you think of this implementation of a* ?
Denis Lisov
@tanriol
Nov 20 2017 21:34
@mnivoliez The datastructure looks a bit strange for me...
let x = goal.pos.x + start.pos.x; - is the + here correct?
mnivoliez
@mnivoliez
Nov 20 2017 21:34
should be a -
thanks :)
Denis Lisov
@tanriol
Nov 20 2017 21:36
let mut current = *f_score.keys().last().unwrap(); - what exactly are you trying to get here?
mnivoliez
@mnivoliez
Nov 20 2017 21:37
one node. any will do. It's the first one I could have think of
Denis Lisov
@tanriol
Nov 20 2017 21:38
Better to take the first one - to execute last(), you iterate over all of them.
mnivoliez
@mnivoliez
Nov 20 2017 21:38
I have base my code on the wikipedia definition of a*
Denis Lisov
@tanriol
Nov 20 2017 21:41
What's worse is that you're partially missing the point...
mnivoliez
@mnivoliez
Nov 20 2017 21:42
ar
not the goal, indeed
Denis Lisov
@tanriol
Nov 20 2017 21:43
...if you care about performance, a HashMap for f_score is not a good choice.
mnivoliez
@mnivoliez
Nov 20 2017 21:44
I'm not at that level yet. I got yet a little understanding of hasMap it self, it's the first map I came accros
Ilya Bogdanov
@vitvakatu
Nov 20 2017 21:44
@tanriol what data structure you're suggesting instead?
mnivoliez
@mnivoliez
Nov 20 2017 21:45
What i'm trying to do is making astar and understand its concept
for the record, it's also a work I have to do for school
and to be honest, I should have had one more week to do it. But the course was move to tomorrow so I'm doing it on evening
Denis Lisov
@tanriol
Nov 20 2017 21:47
@vitvakatu A binary heap. The only operations used in A* are insert and find the minimal value.
mnivoliez
@mnivoliez
Nov 20 2017 21:48
I though about it. But I don't quite dig into BTreeMap and HashMap to understand the differences. Excluding the Hash stuff.
Denis Lisov
@tanriol
Nov 20 2017 21:50
Ooh... and you seem to have a bug, yeah. Does it actually work or just loops forever?
mnivoliez
@mnivoliez
Nov 20 2017 21:51
didn't test it out yet
Denis Lisov
@tanriol
Nov 20 2017 21:53
Note that in the Wiki current := the node in openSet having the lowest fScore[] value, but the part about "in openSet" is not ensured by your code...
mnivoliez
@mnivoliez
Nov 20 2017 21:58
effectively. I zap this.
I should replace the line with *score.keys.first
is there a way to get "elegantly" the key from a map with the lowest value?
oh. I fact I covered the case
with the for next line
not really elegant
I added this to the if: rust open_set.contains(&key)
Denis Lisov
@tanriol
Nov 20 2017 22:04
But you're still iterating the whole f_score map, aren't you?
There's no elegant way to get a key with the lowest value from a map, but you don't actually need a map here.
mnivoliez
@mnivoliez
Nov 20 2017 22:09
maybe a Vec with same indice as the vec<Tile>