rust

Rust: Method “poll” not found in `impl std::future::Future`

吃可爱长大的小学妹 提交于 2021-02-05 07:11:48
问题 I'm trying to learn async programming, but this very basic example doesn't work: use std::future::Future; fn main() { let t = async { println!("Hello, world!"); }; t.poll(); } Everything I've read from the specs says this should work, but cargo complains that method "poll" can't be found in "impl std::future::Future". What am I doing wrong? 回答1: poll has this signature: fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>; There are two problems with calling this in the

Rust: Method “poll” not found in `impl std::future::Future`

本秂侑毒 提交于 2021-02-05 07:10:23
问题 I'm trying to learn async programming, but this very basic example doesn't work: use std::future::Future; fn main() { let t = async { println!("Hello, world!"); }; t.poll(); } Everything I've read from the specs says this should work, but cargo complains that method "poll" can't be found in "impl std::future::Future". What am I doing wrong? 回答1: poll has this signature: fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>; There are two problems with calling this in the

Is there a way to enforce that a Rust raw pointer is not used after returning from a specific stack frame?

那年仲夏 提交于 2021-02-05 06:43:27
问题 I'm writing a Rust wrapper for a (mostly C-style) C++ plug-in SDK. The plug-in host is a graphical desktop application that runs an event loop. The plug-in is regularly called as part of that event loop. Whenever this happens, the plug-in has control and can call arbitrary host functions. One C function which I want to wrap returns a raw pointer. Right after that function returns, the pointer is guaranteed to be a valid C string, so it is safe to dereference it. However, after the plug-in

How can I avoid dynamic dispatch?

落爺英雄遲暮 提交于 2021-02-05 06:42:06
问题 I have the following trait: struct ArtistInfo { // some fields } pub trait Fetcher { fn fetch(&self, artist: String) -> ArtistInfo; } I want to have several different fetchers that I can use under different circumstances. My first instinct is to reach for a map and use trait objects like so: type MusicService = String; let fetchers: HashMap<MusicService, Box<Fetcher>> = HashMap::new(); This will allow me to make the set of available music services configurable at run time. This will result in

How can I better store a string to avoid many clones?

孤街浪徒 提交于 2021-02-05 06:31:21
问题 I am using tokio's UdpCodec trait: pub trait UdpCodec { type In; type Out; fn decode(&mut self, src: &SocketAddr, buf: &[u8]) -> Result<Self::In>; fn encode(&mut self, msg: Self::Out, buf: &mut Vec<u8>) -> SocketAddr; } My associated type for In is a (SocketAddr, Vec<Metric>) . Metric is defined as: #[derive(Debug, PartialEq)] pub struct Metric { pub name: String, pub value: f64, pub metric_type: MetricType, pub sample_rate: Option<f64>, } I have used owned strings to avoid lifetime

How can I better store a string to avoid many clones?

末鹿安然 提交于 2021-02-05 06:31:08
问题 I am using tokio's UdpCodec trait: pub trait UdpCodec { type In; type Out; fn decode(&mut self, src: &SocketAddr, buf: &[u8]) -> Result<Self::In>; fn encode(&mut self, msg: Self::Out, buf: &mut Vec<u8>) -> SocketAddr; } My associated type for In is a (SocketAddr, Vec<Metric>) . Metric is defined as: #[derive(Debug, PartialEq)] pub struct Metric { pub name: String, pub value: f64, pub metric_type: MetricType, pub sample_rate: Option<f64>, } I have used owned strings to avoid lifetime

Rust equivalent to Swift's extension methods to a protocol?

人走茶凉 提交于 2021-02-05 06:30:45
问题 In Swift I can attach extension methods to any of struct , enum or protocol (same with trait in Rust). protocol Foo1 { func method1() -> Int } extension Foo1 { func method2() { print("\(method1())") } } Then all types conforming the protocol Foo1 now all have method2() . This is very useful to build "method chaining" easily. How to do same in Rust? This doesn't work with an error. struct Kaz {} impl Foo for Kaz {} trait Foo { fn sample1(&self) -> isize { 111 } } impl Foo { fn sample2(&self) {

List all traits implemented by a type in a scope

北慕城南 提交于 2021-02-05 04:58:56
问题 For the sake of making debugging easier and such, I would like to know all traits implemented for a type within a certain scope. Can I get rustc to provide me this information? If so, how? 回答1: Use rustdoc / cargo doc . rustdoc creates a section with all trait implementations for a given type. For example with Vec: If you would like to do this for your own crate, you might find --document-private-items to be useful. See also How to generate documentation for private items. 来源: https:/

Differences between `fn` and `||` in type for an array of functions

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-05 02:52:10
问题 I was doing some simple experiments in Rust, involving an array of functions, and finally came out with this working code: fn fun1(arg: &String) -> String { let prefix = String::from_str("a: "); prefix.add(arg) } fn fun2(arg: &String) -> String { let prefix = String::from_str("b: "); prefix.add(arg) } fn doall(arg: &String, funcs_vec: &[fn(&String) -> String]) { for f in funcs_vec.iter() { println!("{}", (*f)(arg)); } } static funcs: &'static [fn(&String) -> String] = &[fun1, fun2]; fn main()

Is it possible to define structs at runtime or otherwise achieve a similar effect?

风格不统一 提交于 2021-02-04 22:17:27
问题 I want to create a function (for a library) which will output a struct for any CSV which contains all the columns and their data. This means that the column names (unless explicitly provided by the user) will not be known until runtime. Is it possible to create a struct definition at runtime or mutate an existing struct? If so, how? For example, how can I mutate the following struct structure: struct Point { x: String, y: String, } To the following (in memory only): struct Point { x: String,