rust

How to infer the return type of a function? [duplicate]

我们两清 提交于 2020-03-21 16:09:47
问题 This question already has an answer here : Differences in Type inference for closures and functions in rust (1 answer) Closed 2 years ago . The return type of a block is inferred. fn main() { let x = { 5 }; println!("{}", x); } But when I give the block a name, I have to specify a type. fn five() -> i32 { 5 } fn main() { let x = five(); println!("{}", x); } How can I avoid selecting a type? 回答1: You cannot. Rust explicitly prohibits this by design. However, for large and complex return types,

In order to downcast a trait object why does it have to be static?

狂风中的少年 提交于 2020-03-19 03:36:11
问题 The std::error::Error trait is transiting to a new design. Noticeably its cause method: fn cause(&self) -> Option<&dyn Error> is deprecated and to be replaced by: fn source(&self) -> Option<&(dyn Error + 'static)> Besides the name, the only change is that the new method returns 'static . The reason given by fix error RFC is: The problem with the existing cause API is that the error it returns is not 'static . This means it is not possible to downcast the error trait object, because

In order to downcast a trait object why does it have to be static?

微笑、不失礼 提交于 2020-03-19 03:34:07
问题 The std::error::Error trait is transiting to a new design. Noticeably its cause method: fn cause(&self) -> Option<&dyn Error> is deprecated and to be replaced by: fn source(&self) -> Option<&(dyn Error + 'static)> Besides the name, the only change is that the new method returns 'static . The reason given by fix error RFC is: The problem with the existing cause API is that the error it returns is not 'static . This means it is not possible to downcast the error trait object, because

Setting the include path with bindgen

痞子三分冷 提交于 2020-03-18 11:22:56
问题 I'm writing a Rust interface to a small C library, which has headers spread in a few locations. It's not a system library, and is normally used by some executables in the same package; I'm currently including it as a git submodule in my Cargo project. Building the library seems to be pretty easy; I've opted to use the gcc crate from build.rs : gcc::Config::new() .file("external/foo/dir1/file1.c") .file("external/foo/dir2/file2.c") .include("external/foo/dir1/") .include("external/foo/dir2/")

Borrowed value does not live long enough, moved due to use in closure E0597

可紊 提交于 2020-03-17 03:17:04
问题 I am taking my first steps on Actix-Web. But this closure causes me errors #[derive(Deserialize, Serialize, Debug, Copy, Clone)] pub struct PaginationQuery { pub limit: Option<u32>, pub offset: Option<u32>, } pub fn get_all_trainings_2( query: web::Query<PaginationQuery>, pool: web::Data<Pool>, ) -> impl Future<Item = HttpResponse, Error = Error> { let mut pagination = query.0; // Thread Blocking web::block(move || database::get_exercises(pool, pagination)).then(|res| { match res { Ok(

Borrowed value does not live long enough, moved due to use in closure E0597

限于喜欢 提交于 2020-03-17 03:16:42
问题 I am taking my first steps on Actix-Web. But this closure causes me errors #[derive(Deserialize, Serialize, Debug, Copy, Clone)] pub struct PaginationQuery { pub limit: Option<u32>, pub offset: Option<u32>, } pub fn get_all_trainings_2( query: web::Query<PaginationQuery>, pool: web::Data<Pool>, ) -> impl Future<Item = HttpResponse, Error = Error> { let mut pagination = query.0; // Thread Blocking web::block(move || database::get_exercises(pool, pagination)).then(|res| { match res { Ok(

Do optional dependencies get enabled by default?

戏子无情 提交于 2020-03-17 03:12:06
问题 If I define a dependency like foo = { version = "1.0.0", optional = true } , will it be available when I do "cargo run"? Can I check if it is enabled in the code? if cfg!(feature = "foo") {} Doesn't seem to be working, like the feature is missing all the time. 回答1: Moving answer to 60258216 here: Optional dependencies do double as features: https://stackoverflow.com/a/39759592/8182118 They will not be enabled by default unless they're listed in the default feature, though you can enable the

Do optional dependencies get enabled by default?

十年热恋 提交于 2020-03-17 03:10:51
问题 If I define a dependency like foo = { version = "1.0.0", optional = true } , will it be available when I do "cargo run"? Can I check if it is enabled in the code? if cfg!(feature = "foo") {} Doesn't seem to be working, like the feature is missing all the time. 回答1: Moving answer to 60258216 here: Optional dependencies do double as features: https://stackoverflow.com/a/39759592/8182118 They will not be enabled by default unless they're listed in the default feature, though you can enable the

How to convert a usize to a single char?

让人想犯罪 __ 提交于 2020-03-16 09:15:30
问题 For an exercise I'm doing for exercism (the minesweeper task), I need to convert an usize to a char in order to insert it into a std::string::String . To describe the problem in minimal lines of code: let mut s = String::from(" "); let mine_count: usize = 5; // This is returned from a method and will be a value between 1 and 8. s.insert(0, _______); // So I get: "5 " at the underscores I do: The way I'm currently doing this as: mine_count.to_string().chars().nth(0).unwrap(); // For example:

How do I fix mismatching dependencies in my Cargo file to work around native library collisions?

巧了我就是萌 提交于 2020-03-16 06:49:38
问题 I'm setting up a Rust server with Rocket and I'm trying to use it with a JWT library. They use different versions of the *ring* crate and I get an error during cargo build : error: multiple packages link to native library `ring-asm`, but a native library can be linked only once package `ring v0.12.1` ... which is depended on by `jsonwebtoken v4.0.1` ... which is depended on by `auther v0.1.0 (file:///home/drpytho/x/downloadble/auther)` links to native library `ring-asm` package `ring v0.11.0`