rust

How to use std::convert::Into in statements in Rust? [duplicate]

放肆的年华 提交于 2020-02-25 02:59:05
问题 This question already has answers here : Using .into() when type inference is impossible (4 answers) Closed last year . I am trying to use into in the following code this way: use std::convert::{From, Into}; struct MyStruct { x: i64, } impl From<i64> for MyStruct { fn from(a: i64) -> Self { Self { x: a } } } impl Into<i64> for MyStruct { fn into(self) -> i64 { self.x } } fn main() { let s = MyStruct::from(5); let b = s.into() == 5; println!("{:?}", b); } It produces an error: error[E0283]:

C to Rust and back “pointer being freed was not allocated” on Rust side

对着背影说爱祢 提交于 2020-02-25 02:23:53
问题 I'm trying to call some Rust code from C and get a result back, but I'm getting a "pointer being freed was not allocated" error on the Rust side. I want to call the hex::encode function. I pass in a pointer to some bytes, the length, and a pointer allocated in C with malloc. I want the result of the conversion passed back at this pointer. The Rust function: extern crate libc; use hex; use std::ffi::CString; use std::os::raw::c_char; use std::vec::Vec; use std::{ffi, ptr, slice}; #[no_mangle]

C to Rust and back “pointer being freed was not allocated” on Rust side

痞子三分冷 提交于 2020-02-25 02:22:07
问题 I'm trying to call some Rust code from C and get a result back, but I'm getting a "pointer being freed was not allocated" error on the Rust side. I want to call the hex::encode function. I pass in a pointer to some bytes, the length, and a pointer allocated in C with malloc. I want the result of the conversion passed back at this pointer. The Rust function: extern crate libc; use hex; use std::ffi::CString; use std::os::raw::c_char; use std::vec::Vec; use std::{ffi, ptr, slice}; #[no_mangle]

How to declare a const String in stable Rust? [duplicate]

余生颓废 提交于 2020-02-24 12:09:46
问题 This question already has answers here : What are the differences between Rust's `String` and `str`? (8 answers) Trying to declare a String const results in expected type, found “my string” (1 answer) Closed 2 years ago . I am trying to declare a const String in stable Rust but it does not let me declare it: const CONSTANT_VALUE: String = String::from("constant value"); fn main() { println!("{}", TARGET_PORT_KEY); } It is saying that: Calls in constants are limited to tuple structs and tuple

How to declare a const String in stable Rust? [duplicate]

点点圈 提交于 2020-02-24 12:08:44
问题 This question already has answers here : What are the differences between Rust's `String` and `str`? (8 answers) Trying to declare a String const results in expected type, found “my string” (1 answer) Closed 2 years ago . I am trying to declare a const String in stable Rust but it does not let me declare it: const CONSTANT_VALUE: String = String::from("constant value"); fn main() { println!("{}", TARGET_PORT_KEY); } It is saying that: Calls in constants are limited to tuple structs and tuple

How to declare a const String in stable Rust? [duplicate]

喜欢而已 提交于 2020-02-24 12:08:08
问题 This question already has answers here : What are the differences between Rust's `String` and `str`? (8 answers) Trying to declare a String const results in expected type, found “my string” (1 answer) Closed 2 years ago . I am trying to declare a const String in stable Rust but it does not let me declare it: const CONSTANT_VALUE: String = String::from("constant value"); fn main() { println!("{}", TARGET_PORT_KEY); } It is saying that: Calls in constants are limited to tuple structs and tuple

How to declare a const String in stable Rust? [duplicate]

↘锁芯ラ 提交于 2020-02-24 12:06:12
问题 This question already has answers here : What are the differences between Rust's `String` and `str`? (8 answers) Trying to declare a String const results in expected type, found “my string” (1 answer) Closed 2 years ago . I am trying to declare a const String in stable Rust but it does not let me declare it: const CONSTANT_VALUE: String = String::from("constant value"); fn main() { println!("{}", TARGET_PORT_KEY); } It is saying that: Calls in constants are limited to tuple structs and tuple

How do I share a HashMap between Hyper handlers?

最后都变了- 提交于 2020-02-24 11:16:13
问题 I'm attempting to learn Rust by implementing a simple in-memory URL shortener with Hyper 0.10. I'm running into an issue that I think is caused by trying to close over a mutable HashMap in my handler: fn post(mut req: Request, mut res: Response, short_uris: &mut HashMap<&str, &str>) { let mut body = String::new(); match req.read_to_string(&mut body) { Ok(_) => { let key = short_uris.len(); short_uris.insert(&key.to_string(), &body.to_string()); *res.status_mut() = StatusCode::Created; res

How many lines are covered by the Rust conditional compilation attribute?

牧云@^-^@ 提交于 2020-02-24 09:14:05
问题 I'm trying to use a conditional compilation statement. Beyond defining a function that should only exist in a debug build, I want to define a set of variables/constants/types that only exist in the debug build. #[cfg(debug)] pub type A = B; pub type B = W; #[cfg(other_option)] pub type A = Z; pub type B = I; let test = 23i32; How many lines are actually "covered" by the conditional compile attribute in this case? Is it only one (what I would expect in this context)? Are there ways to ensure

Using async/await with old `Future<Item = X, Error = Y>` types [duplicate]

瘦欲@ 提交于 2020-02-24 05:00:06
问题 This question already has an answer here : Is there a way that we can convert from futures 0.1 to the standard library futures? (1 answer) Closed 24 days ago . I have a function in a crate that returns old style futures. Imagine something like: pub fn old_function() -> impl Future<Item = X, Error = Y> ... I want to use this crate in a new codebase where I don't want to mix things too much. How can I keep the new implementation clean and use async/await when calling this old_function ? 回答1: