lifetime

What kind of lifetime parameter do I have to use here when declaring a struct field object type

試著忘記壹切 提交于 2019-12-01 19:32:09
This is what my code looks like. I'm trying to use a impled struct within my ShapeRenderer struct and use its methods. shapes.rs: use super::core::*; pub struct ShapeRenderer<'a> { core_renderer: &'a mut CanvasRenderer, } core.rs pub struct Canvas { pub width: usize, pub height: usize, pub array: Vec<char>, } pub struct Point { pub x: usize, pub y: usize, } pub struct CanvasRenderer<'a> { canvas: &'a mut Canvas, } impl<'a> CanvasRenderer<'a> { pub fn new(canvas: &'a mut Canvas) -> CanvasRenderer { CanvasRenderer { canvas: canvas } } } Error error[E0107]: wrong number of lifetime parameters:

How to specify lifetime bounds for a closure involving references to intermediate local variables?

对着背影说爱祢 提交于 2019-12-01 17:34:09
问题 I'm trying to write a function like the following in Rust: fn double_and_square<'a, T>(x: &'a T) -> /* whatever the output type of `&t * &t` is */ { let t = x + x; &t * &t } I want it to work on types where T is non- Copy . I need to specify not only that &'a T implements Add (easy), but also that a reference to its output type with the lifetime of local variable t implements Mul . Attempt #1 (no lifetime specified for the intermediate type): fn double_and_square<'a, T>(x: &'a T) -> <&<&'a T

C++ temporary variable lifetime

别来无恙 提交于 2019-12-01 17:19:50
问题 Is this code valid? int foo() { std::vector<std::string>& v = std::vector<std::string>(5, "X"); // Do something silly... return 42; } For some reason I thought that the temporary std::vector object (right from the assignment sign) should be destructed right after it's construction (thus rendering the reference invalid) . However, debugging proves that I'm wrong and, well, I realized that I don't quite understand why does the temporary variable is destructed when the function returns. I guess

Exception object lifetime

非 Y 不嫁゛ 提交于 2019-12-01 16:13:44
I want to know how the exception object is created ? and why the handler function parameter can be a non-const reference? For example: class E{ public: const char * error; E(const char* arg):error(arg){ cout << "Constructor of E(): ";} E(const E& m){ cout << "Copy constructor E(E& m): " ; error=m.error; } }; int main(){ try{ throw E("Out of memory"); } catch(E& e){cout << e.error;} } Output: Constructor of E(): Out of memory so I have throw E("out of memory") and E("out of memory") is just a temporary object and no object has been created except E("out of memory") because no copy constructor

Dnspython: Setting query timeout/lifetime

馋奶兔 提交于 2019-12-01 15:50:33
I have a small script that checks a large list of domains for their MX records, everything works fine but when the script finds a domain with no record, it takes quite a long time to skip to the next one. I have tried adding: query.lifetime = 1.0 or query.timeout = 1.0 but this doesn't seem to do anything. Does anyone know how this setting is configured? My script is below, thanks for your time. import dns.resolver from dns.exception import DNSException import dns.query import csv domains = csv.reader(open('domains.csv', 'rU')) output = open('output.txt', 'w') for row in domains: try: domain =

Dnspython: Setting query timeout/lifetime

前提是你 提交于 2019-12-01 15:38:32
问题 I have a small script that checks a large list of domains for their MX records, everything works fine but when the script finds a domain with no record, it takes quite a long time to skip to the next one. I have tried adding: query.lifetime = 1.0 or query.timeout = 1.0 but this doesn't seem to do anything. Does anyone know how this setting is configured? My script is below, thanks for your time. import dns.resolver from dns.exception import DNSException import dns.query import csv domains =

Implementing a trait for closures results in bound/concrete lifetime mismatch

梦想与她 提交于 2019-12-01 15:09:34
问题 I want to implement a trait for closures of a specific type. Here is a minimal example (playground): trait Foo { fn foo(&self, x: &u32); } impl<F> Foo for F where F: Fn(&u32) { fn foo(&self, x: &u32) { self(x) } } fn main() { let _: &FnOnce(&u32) = &|x| {}; // works let _: &Foo = &|x| {}; // doesn't work } It results in this error: error: type mismatch resolving `for<'r> <[closure@<anon>:16:29: 16:35] as std::ops::FnOnce<(&'r u32,)>>::Output == ()`: expected bound lifetime parameter , found

Why does my saved D3 selection have no effect in some cases?

依然范特西╮ 提交于 2019-12-01 12:43:59
I'm confused about how to save a D3 selection for later use. In the code below, I have a "global" variable for my axes, to which I save them when they are first created. Later, I'm able to use this variable for certain things (here, setting some text) but not others (here, updating the scale), which only work if I explicitly (re)select the axes. Is there perhaps something about JavaScript variable scoping or lifetime that I don't understand? Any assistance is appreciated! The relevant code, much condensed from the full context : // Top level of page var gxaxis, gyaxis; var updatePlot =

Why does my saved D3 selection have no effect in some cases?

雨燕双飞 提交于 2019-12-01 11:37:31
问题 I'm confused about how to save a D3 selection for later use. In the code below, I have a "global" variable for my axes, to which I save them when they are first created. Later, I'm able to use this variable for certain things (here, setting some text) but not others (here, updating the scale), which only work if I explicitly (re)select the axes. Is there perhaps something about JavaScript variable scoping or lifetime that I don't understand? Any assistance is appreciated! The relevant code,

What does the 'static lifetime mean in a trait bound in a Rust future? [duplicate]

前提是你 提交于 2019-12-01 11:03:46
This question already has an answer here: Why does the Rust compiler request I constrain a generic type parameter's lifetime (error E0309)? 1 answer Why is the bound `T: 'a` required in order to store a reference `&'a T`? 2 answers The compiler suggests I add a 'static lifetime because the parameter type may not live long enough, but I don't think that's what I want 2 answers I thought that I've got what the 'static lifetime is, but now I'm not sure. I'm trying to understand how to work with Tokio and Futures. My app is working, but the structure is awful, so I need to decompose it. Here comes