()
found type my_mod::MyProps
"?
;
at the end of html! { ... };
looks incorrect
MyComponent
?
BUG: rustopenssl lock 1 already unlocked, aborting
Got a question w.t.r closures and return types  Rust Playground
fn with_x<F: FnOnce(&mut i64)>(n: i64, f:F){
let mut x:i64 = 5 + n;
f(&mut x)
}
fn main() {
let mut y = 0;
with_x(3, x y = *x + 1);
println!("{}",y);
}
Is there a way to obtain the result of the x *x + 1
directly without having to declare a let mut y=0
first?
i.e
let y = with_x(3, x *x + 1);
fn with_x<F: FnOnce(i64) > i64>(n: i64, f:F) > i64 {
let mut x:i64 = 5 + n;
f(x)
}
fn main() {
let mut y = with_x(3, x x + 1);
println!("{}", y);
}
<F: FnOnce(i64) > i64>
is the return type of x x + 1
i.e f(x)?
FnOnce(ArgumentType) > ReturnType
fn with_x<F: FnOnce(i64) > i64>(n: i64, f:F) > i64 {
let mut x:i64 = 5 + n;
f(x)
}
fn sq(x:i64) > i64 {x * x}
fn try_sq(x:i64) > Option<i64> { Some(x * x) }
fn main() {
let mut y = with_x(3, sq);
let mut z = with_x(3, x try_sq(x).unwrap());
println!("y {}, z {}", y, z);
}
with_x
so that it can handle a return general type T
F: FnOnce(i64) > T
?
fn with_x<F: FnOnce(T) > T>(n: T, f:F) > T { ... }
T
fn with_x<F: FnOnce(i64) > T>(n: i64, f:F) > T { ... }
if argument type is sill i64
x:i64
and Some(x:i64)
as return types from f(x)
fn with_x<F: FnOnce(i64) > T>(n: i64, f:F) > T {
let x = 5 + n;
f(x);
}
Should work for any T
T
is any type?
where T: ...
then T
can be any type
Compiling playground v0.0.1 (file:///playground)
error[E0412]: cannot find type `T` in this scope
> src/main.rs:4:29

4  fn with_x<F: FnOnce(i64) > T>(n: i64, f:F) > T {
 ^ did you mean `F`?
T
before F: FnOnce
<T>
fn with_x<T, F: FnOnce(i64) > T>
T
is typeparameter. It must be introduced before usage
where T:
I can specify what possible types T can be right?
T
must satisfy
T: Clone
means T
must be clonnable
F: FnOnce(i64) > T
means that F
is a any callable that takes i64
and returns T

55  self.with_selem(alsa::mixer::Selem::get_playback_volume_range);
 ^^^^^^^^^^
 
 expected signature of `for<'r, 's> fn(&'r mut alsa::mixer::Selem<'s>) > _`
 found signature of `for<'r> fn(&'r alsa::mixer::Selem<'_>) > _`
with_selem
expets a function that can take reference with arbitrary lifetime to the value bound to arbitrary lifetime. But the function get_playback_volume_range
only work for same arbitrary lifetime at reference and type
with_selem
is the function you write
F: FnOnce(&Selem)
for<'r, 's> F: FnOnce(&'r Selem<'s>)
F: for<'r> FnOnce(&'r Selem<'r>)
for
here is for the lifetimes?
for<'a>
is the construction to declare "arbitrary lifetime" named 'a
'r
F
must implement a trait named FnOnce
with parameter &'r Selem<'r>
"
with_x
)let y = with_x(3, sq);
and let y = with_x(3, z sq(z));
Okay, let me see if I can now fix my actual problem with this new sense of understanding!
 55  self.with_selem(alsa::mixer::Selem::get_playback_volume_range);  ^^^^^^^^^^    expected signature of `for<'r, 's> fn(&'r mut alsa::mixer::Selem<'s>) > _`  found signature of `for<'r> fn(&'r alsa::mixer::Selem<'_>) > _`
But if there is no difference, how comeself.with_selem( selem selem.get_playback_volume_range())
works?
as an analogy, this is somewhat similar to
fn f(x: u64) {}
fn main() {
f(10);
}
working but
fn f(x: u64) {}
fn main() {
let x: u32 = 10;
f(x);
}
not
for<'r, 's> FnOnce(&'r Selem<'s>)
and calls function with smallest lifetime.
Box<Bridge<my_mod::MyMod>>
? self.mymod.modmember
gives an error of unknown field for modmember
although the struct member indeed exists, and therefore I suspect the problem is in how I'm accessing through a Boxed/Bridged type
To be precise. The closure implements
for<'r, 's> FnOnce(&'r Selem<'s>)
and calls function with smallest lifetime.
So it works as adaptor. But since lifetimes are erased after borrowchecker there is no difference in generated machine code
Okay  in that case, will use the closure syntax :)
Box
implements Deref
so you can access Bridge
's fields directly
MyMod
is a generic paramter, without knowing how Bridge
defined I can't say anything else about it
Bridge<T>
has field t_field: T
then you should write boxed_bridge.t_field.my_mod_member
Bridge
boxed_bridge.send(input)
Box<dyn TraitName>
btw
&str
vs String
muddle? def f<S: Into<String>>(s: S) > { whatever(s.into()) }
works to allow f
to take string literals, but it's pretty ugly. f("herring".to_string())
everywhere is worse, though.
&str
as your function parameter, then when you pass it a string, reference it.
&str
or String
is when you conditionally take ownership of the argument. Use Cow<str>
in this case
.into()
.to_string()
but still not ideal.
to_string()
could be replaced with .into()
cause String: From<&str>
S: Into<String>
then should be your choice
.into()
once though
S: Into<Cow<str>>
Cow<str>
not String
.
S: Into<Cow<str>>
will make it possible to not allocate String
if you don't take ownership
String
if you do
Cow<str>
?
&str
form and then just call clone
iff you need to take ownership, then otherwise just leave it as a reference.
context: Box<Bridge<context::Worker>>,
in https://github.com/DenisKolodin/yew should instead be context: Box<dyn Bridge<context::Worker>>,
?
dyn Trait
more explicit and is counterpart for impl Trait
dyn
an abbreviation of?