What's the proper way to get a string slice from a Rc<RefCell<String>>? [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
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];

Related

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

Divide u8 by usize [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to divide u8 and usize, like so:
let number: u8 = 100;
let mut listy = Vec::new();
for x in 0..10 {
listy.push(x);
}
let divi = number / listy.len();
I get
error[E0277]: cannot divide `u8` by `usize`
--> src/main.rs:9:23
|
9 | let divi = number / listy.len();
| ^ no implementation for `u8 / usize`
|
= help: the trait `Div<usize>` is not implemented for `u8`
Is there no way to convert a usize to u8? Also, does the variable divi need to be a float?
Is there a precision float Rust?
You need to make your types match, both u8, usize or whatever you need (going to an upper type. Meaning that a usize can not be transformed to a u8 normally, because it may not fit, but an u8 to usize it could because it is guaranteed it will fit.).
As you ask float:
let div = number as f32 / listy.len() as f32;
Playground

RUST compilation error: Expecting `::` instead got `{` [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
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.

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