RUST compilation error: Expecting `::` instead got `{` [closed] - rust

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am new to rust, and I am trying to compile my code with this function:
fn recieve(&mut self, timeout: u64) -> <Vec<Vec<u8>>> {
let mut bufs: Vec<Vec<u8>> = self.streams.iter().map(|_| Vec::new()).collect();
task::block_on(future::join_all(self.streams.iter_mut().zip(bufs.iter_mut()).map(|(s, b)| {
io::timeout(Duration::from_secs(timeout), s.read_to_end(b))
})));
Ok(bufs)
}
I get this error:
error: expected `::`, found `{`
--> src/lib.rs:60:59
|
60 | fn recieve(&mut self, timeout: u64) -> <Vec<Vec<u8>>> {
| ^ expected `::`
What is the issue? Can someone point me in the right direction?

Lose the angle-brackets surrounding the return type, and you should get past this problem.
Angle-brackets are only used in types to surround the argument provided to a generic type - they cannot surround any type like parentheses can surround any expression.

Related

for range on slice on array yields [T] instead of T? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I wand to iterate on part of a slice knowing how many items of that slice i want to iterate on.
How can I achieve that?
What i've tried is to have a for of on a range [0..only] but indexing my slice with the produced index gives me a [T] instead of a T. How come? what i am missing?
fn in_slice(slice: &[u8], only: usize) {
for index in [0..only] {
println!("{}", slice[index]); // slice[index] here is a [u8] and not a u8
}
}
fn main() {
in_slice(&[1, 2, 3], 2)
}
0..only is a Range<usize>. [0..only] is a [Range<usize>; 1], i.e. an array with an element type Range<usize> and length 1.
Therefore, iterating over [0..only] will yield a single element of type Range<usize>.
To iterate over the range and yield usizes, use the range:
for index in 0..only {
println!("{}", slice[index]);
}

How do I use attributes on fields on a custom derive macro? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I'm writing my derive macro:
use proc_macro::{self, TokenStream};
#[proc_macro_derive(MyMacro, attributes(my_attr))]
pub fn my_macro(input: TokenStream) -> TokenStream {
...
TokenStream::from(my_generated_code)
}
with the attributes which will be used like:
use my_crate::MyMacro;
#[derive(MyMacro)]
#[my_attr(foo)]
struct MyStruct {
// some fields
#[my_attr(abc)]
a: i32,
}
But this snip leads to:
error: cannot find attribute `my_attr` in this scope
--> src/main.rs:10:7
|
6 | #[my_attr(abc)]
| ^^^^^^^
This is insanely weird because the attribute on struct is working well. Also I tried to change struct on enum and then field annotation has worked for some reason
May it be the problem of rustc, proc_macro version? rustc version: rustc 1.61.0-nightly (c5cf08d37 2022-03-30)
Macro-crate's Cargo.toml:
[lib]
proc-macro = true
[dependencies]
syn = {version= "1.0", features = ["full", "extra-traits"]}
quote = "1.0"
I found the same problem: issue, but the solution isn't clear at all and it's solved so I can't ask for some explanations

What's the proper way to get a string slice from a Rc<RefCell<String>>? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
use std::{cell::RefCell, rc::Rc};
fn main() {
let wrapped_string = Rc::from(RefCell::from(String::from("hello there my majesty")));
let partial: str = wrapped_string.borrow()[7..18];
}
gives the error:
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/main.rs:5:9
|
5 | let partial: str = wrapped_string.borrow()[7..18];
| ^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= note: all local variables must have a statically known size
= help: unsized locals are gated as an unstable feature
help: consider borrowing here
|
5 | let partial: str = &wrapped_string.borrow()[7..18];
| ^
Change
let partial: str = wrapped_string.borrow()[7..18];
to
let partial: &str = &wrapped_string.borrow()[7..18];

Cant Set Struct Property to Enum Rust [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have a structure textApp with field mode, which has a type of a Mode:
pub struct textApp{
mode: Mode,
}
pub enum Mode {
Single,
Multiple,
}
I initialize a new instance of the textApp structure as myTextApp, and I have a button then when clicked I want it to change the value of myTextApp's .mode field to Mode::Single:
fn main(){
let mut myTextApp = textApp{
mode: Mode::Multiple,
};
singleModeBut.set_callback(move||{
let newMode: Mode = Mode::Single;
textApp.mode = newMode;
//throws an error
});
}
but this gives me the error:
error[E0423]: expected value, found struct `textApp`
| textApp.mode = newModeSet;
| ^^^^^^^-----
| |
| help: use the path separator to refer to an item: `textApp::mode`
Why can't I set this struct field to Mode::Single?
Your struct's instance name is myTextApp. You are trying to set using struct's name which doesn't make sense.
Changing,
textApp.mode = newMode;
to
myTextApp.mode = newMode;
will fix it.
Playground
Also, it's a good idea to follow rust style guidelines that ask you to use snake case for identifier names and upper camel case for type names. That will help you avoid such errors.

In which situations would Stdin::lock be useful? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Stdin::lock:
pub fn lock(&self) -> StdinLock<'_>
In which situations would this be useful?
The documentation states, emphasis mine:
A handle to the standard input stream of a process.
Each handle is a shared reference to a global buffer of input data to this process. A handle can be lock'd to gain full access to BufRead methods (e.g., .lines()). Reads to this handle are otherwise locked with respect to other reads.
use std::io::{self, prelude::*};
fn main() {
let stdin = io::stdin();
dbg!(stdin.lines().count()); // fails!
let stdin = stdin.lock();
dbg!(stdin.lines().count());
}
error[E0599]: no method named `lines` found for struct `std::io::Stdin` in the current scope
--> src/main.rs:6:16
|
6 | dbg!(stdin.lines().count());
| ^^^^^ method not found in `std::io::Stdin`
|
= note: the method `lines` exists but the following trait bounds were not satisfied:
`std::io::Stdin: std::io::BufRead`
which is required by `&mut std::io::Stdin: std::io::BufRead`

Resources