These are chat archives for rust-lang/rust

8th
Jan 2019
aohan237
@aohan237
Jan 08 05:27
how to impl a listNode which can insert node to it
aohan237
@aohan237
Jan 08 05:50
it seems that it is hard to impl a listnode without unsafe code?
how to resolve the ownership issue when you change a listNode
rsb007
@rsb007
Jan 08 06:47
i am trying to convert png image into bytes but not found any suitable things
Tim Robinson
@1tgr
Jan 08 07:36
@aohan237 indeed it's hard to write an efficient linked list in Rust without unsafe code: https://cglab.ca/~abeinges/blah/too-many-lists/book/
aohan237
@aohan237
Jan 08 07:43
yeah...i google around, found no solutions
and i found argus that "this is the limit of rust, if with unsafe why not use c++...."
@1tgr
zodiake
@zodiake
Jan 08 10:03

why this one can be compiled

let vec = vec!(1, 2, 3);
    let a = vec[0];
    println!("{}", a);

But this one can not?

let mut v = Vec::new();
for i in 101 .. 106 {
v.push(i.to_string());
}
// Pull out random elements from the vector.
let third = v[2];
let fifth = v[4]

Anybody can give some help?Thanks

toxicafunk
@toxicafunk
Jan 08 10:05
what error do u get?
its missing a tgrailing ; fwiw :)
zodiake
@zodiake
Jan 08 10:07
sorry for missing ;
but even if you append ;
Michal 'vorner' Vaner
@vorner
Jan 08 10:07
No. Ints are copy, strings are not.
zodiake
@zodiake
Jan 08 10:10
let vec = vec!["a", "b"];
    let a = vec[0];
    println!("{}", a);
this can be compiled
Michal 'vorner' Vaner
@vorner
Jan 08 10:13
"a" is &str, not String.
toxicafunk
@toxicafunk
Jan 08 10:14
so u need let third = &v[2]; instead
right?
zodiake
@zodiake
Jan 08 10:14
"third" is String but not &str?
toxicafunk
@toxicafunk
Jan 08 10:15
string does not have a Copy trait implemented afaiu
so u need a reference to the vector position holding the string
zodiake
@zodiake
Jan 08 10:16
@toxicafunk I know let third=&v[2] but why let a = vec[0]; can work..
toxicafunk
@toxicafunk
Jan 08 10:16
cuz "a" is a &str already
let vec = vec![String::from("a"), String::from("b")];
try this above
it should fail
zodiake
@zodiake
Jan 08 10:18
so there is some different between "vec!" and "Vew::new()"?
Michal 'vorner' Vaner
@vorner
Jan 08 10:18
For non-copy types, the move from that place invalidates the original place. But you can't just invalidate one slot in a vector. On the other hand, copy types are simple enough and it basically means you can make a bit-copy of them, leave the original intact and nothing bad happens. String is owning pointer to a heap, so if you just blindly copied that pointer, it would get freed twice in the end. &str is just a pointer without the freeing (someone else is responsible to free it), so you can make as many copies of that
No, there's a difference between Vec<String> and Vec<&str>.
zodiake
@zodiake
Jan 08 10:21
I got some confused, let vec=vec!["abc"] the type of vec is Vec<&str> ?
toxicafunk
@toxicafunk
Jan 08 10:21
yes
"some string" = &str
u need to use String::from to create a String
zodiake
@zodiake
Jan 08 10:23
Ok,I should reread the official doc .type of String seems hard to understand...Thanks for replying
Michal 'vorner' Vaner
@vorner
Jan 08 10:24
Have you read The Book? I think this is not about the type, but about some more general concepts.
toxicafunk
@toxicafunk
Jan 08 10:24
tbf it took me a while to understand String vs &str
zodiake
@zodiake
Jan 08 10:25
Just begin reading...
aohan237
@aohan237
Jan 08 10:40
@zodiake maybe you should reread : ownership,copy etc
thojest
@thojest
Jan 08 11:44
hey Rustaceans
i am very very new to Rust :) and must say that it is a bit hard in the beginning ^^ i hope this pays off
i have a problem which I find pretty hard to google maybe you can help me
I use a create A which depends on another crate B. But I also need to use crate B in my program
and B is private in A
Michal 'vorner' Vaner
@vorner
Jan 08 11:49
So, just depend both on A and B and don't worry about A also depending on that.
Cargo will figure out that having B only once to satisfy both is enough (unless you and A insist on using incompatible versions, in which case it would just bring in both versions)
thojest
@thojest
Jan 08 11:51
yeah I think that is exactly the problem
im using different versions of the same crate...
Michal 'vorner' Vaner
@vorner
Jan 08 11:52
That shouldn't be a problem as far as A keeps it's version of B private. Or is it throwing some errors? Something you can show?
thojest
@thojest
Jan 08 11:55

```extern crate rusoto_dynamodb;
extern crate rusoto_core;
extern crate serde_dynamodb;

use rusoto_core::Region;
use rusoto_dynamodb::{DynamoDbClient, PutItemInput};

[derive(Serialize, Deserialize)]

struct TableEntry {
address: &'static str
}

pub fn init_dynamo_client() -> DynamoDbClient{
DynamoDbClient::new(Region::UsEast1)
}

pub fn create_put_item(table: &str, ethaddress: &str) -> PutItemInput {
PutItemInput{
item: serde_dynamodb::to_hashmap(&TableEntry{
address: ethaddress
}).unwrap(),
table_name: table.to_string(),
..Default::default()
}
}```

the error is
error[E0308]: mismatched types
--> src/dbhelper.rs:20:15
|
20 | item: serde_dynamodb::tohashmap(&TableEntry{
| __
^
21 | | address: ethaddress
22 | | }).unwrap(),
| |___^ expected struct dbhelper::rusoto_dynamodb::AttributeValue, found struct rusoto_dynamodb::generated::AttributeValue
|
= note: expected type std::collections::HashMap<_, dbhelper::rusoto_dynamodb::AttributeValue>
found type `std::collections::HashMap<_, rusoto_dynamodb::generated::AttributeValue>
```
sry for messing up format
Michal 'vorner' Vaner
@vorner
Jan 08 11:59
I see, so it is not completely private, a reexported type is sticking out. I guess you can't pick another version of the indirect dependency, but you can select version of your own dependency to match.
thojest
@thojest
Jan 08 12:00
will do that thx
Michal 'vorner' Vaner
@vorner
Jan 08 12:01
Also, if the indirect dependency is out of date, open an issue there if they could update it.
toxicafunk
@toxicafunk
Jan 08 12:43
``` [shift+enter] ... code ... [shift + enter] ```
Sergey Nikolaev
@qvantor
Jan 08 13:36
struct Rect {
    id: u32,
    name: String,
    color: Color
}
impl ObjectTR for Rect {}
impl SomeOtherTrait for Rect {
    fn id(&self) -> u32
}
struct Collection {
    objects: Vec<Box<ObjectTR>> // I sad here that in Vec should be obj witch is impl ObjectTR trait
// But object inside Vec can also impl SomeOtherTrait, or any other trait 
}
let collect = Collection { objects: vec![] }
collect.push(Box::new(Rect::new()))
a.objects[0].id() // i can't call fn from SomeOtherTrait here, only from ObjectTR trait
How to use methods from all available traits inside vector? If different object impl different traits, but all object impl ObjectTR trait?