These are chat archives for rust-lang/rust

28th
Dec 2018
Sergey Nikolaev
@qvantor
Dec 28 2018 13:04

Today my first day in rust, and i can't get one thing. Look:

#[macro_use]extern crate rocket;
#[macro_use]extern crate rocket_contrib;
#[macro_use]extern crate serde_derive;

use rocket_contrib::json::{Json};

#[derive(Deserialize, Serialize)]
struct Person {
    name: String,
    age: u8
}

#[derive(Serialize)]
struct Response {
    status: u8,
    name: String,
    age: u8
}

#[post("/", format = "json", data = "<data>")]
fn post_create(data: Json<Person>) -> Json<Response> {
    println!("We got {}, {} years old", data.name, data.age);
    let a = Response { status: 200, name: data.name, age: data.age }; // throw cannot move out of borrowed content for name, but age is ok. Why? And how to fix name?
    Json(a)
}

throw cannot move out of borrowed content for name, but age is ok. Why? And how to fix name?

Tim Robinson
@1tgr
Dec 28 2018 13:07
name: data.name.clone()
u8 is Copy, meaning that taking a copy of data.age is zero cost
String is not Copy but it is Clone, meaning taking a copy of data.name is possible only by calling .clone()
Sergey Nikolaev
@qvantor
Dec 28 2018 13:10
sound reasonable, but what if i want to send a link on this string, without new clone?
Sergey Nikolaev
@qvantor
Dec 28 2018 13:17
Do I right that in RUST, struct + impl should be used instead of Class?
Michal 'vorner' Vaner
@vorner
Dec 28 2018 13:19
That's probably the closest thing Rust has to a class, yes. But you should remember that Rust is not object oriented language and doesn't have a direct analogy to that.
Sergey Nikolaev
@qvantor
Dec 28 2018 13:22
Rust is more functional language? Or even just procedural?
Michal 'vorner' Vaner
@vorner
Dec 28 2018 13:23
Somewhere in between, not really in either of the camps directly.
You'll notice bits of each, but going fully functional or fully OOP will be painful.
Sergey Nikolaev
@qvantor
Dec 28 2018 13:24
So should I use functional abstractions?
For extending functionality, for example?
Michal 'vorner' Vaner
@vorner
Dec 28 2018 13:26
Wherever appropriate. There's not really a hard rule when to use what.
Sergey Nikolaev
@qvantor
Dec 28 2018 13:27
Sound like i do in JS, using partially OOP and Functional ways
I just JS developer, who want more performance with Wasm + RUST
Michal 'vorner' Vaner
@vorner
Dec 28 2018 13:29
Then you best just spend some time reading the book and trying things out. After enough lines of code that'll feel ugly and forced you'll get the feeling for what looks right ;-)
Sergey Nikolaev
@qvantor
Dec 28 2018 13:33
Thank you :)
David O'Connor
@David-OConnor
Dec 28 2018 20:04
Do y'all know if there's a way to use pub use in Rust 2018? (ie expose funcs etc from a module in the main crate, for APIs) Current workaround for me is thin wrapper funcs
Denis Lisov
@tanriol
Dec 28 2018 20:05
Does it not work?
Zakarum
@omni-viral
Dec 28 2018 20:14
@David-OConnor You just need to use crate keyword
pub use crate::{
  modulename1::{
    funcname1, funcname2,
  },
  modulename2::{
    funcname3,
  },
};
Or self instead of crate if modulename is inside the module you write this pub use
For lib.rs both self and crate will mean the same