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

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`

Related

To print simple struct or enum, derived from Debug or explicit fmt::Display implementation? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last month.
Improve this question
I have a simple enum or struct and want to print. We know that being derived from Debug automatically enables a quick printing without explicit an implementation of fmt::Display. But you can still implement Display for printing it.
#[derive(Clone, Copy, PartialEq)]
pub enum MyDataType {
INVALID = 0,
TYPE1 = 1,
TYPE2 = 2,
}
let my_type: MyDataType = ...;
// This won't compile.
println!("{my_type}");
|
10 | println!("{my_type}");
| ^^^^^^^ `MyDataType` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `MyDataType`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
So, there're two approaches to make it work:
// Simply using Debug
#[derive(Debug)]
// Or, have your own Display implementation
impl fmt::Display for MyDataType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MyDataType::INVALID => write!(f, "INVALID"),
MyDataType::TYPE1 => write!(f, "TYPE1"),
MyDataType::TYPE2 => write!(f, "TYPE2"),
}
}
}
What are the tradeoffs between these two approaches? Which one do you prefer?
Your question seems to imply there's only two options, derive Debug or manually implement Display, which is not the case at all, you can do both (or neither) and for different reasons. Debug and Display are different traits for different purposes; Debug is developer-centric while Display is user-centric.
If the derived implementation is good enough for developers, great! You can use that! If not, you can implement it manually. And if that is also good enough for users, great! You can defer to the Debug implementation when implementing Display as #cdhowie suggested in the comments. If not, you can implement that manually as well.
I don't see any "tradeoffs" to really consider here. If the derived implementation isn't what you want, then don't use it. Its up to you what you need and how it should be formatted.

Convert iterator of string slice to Vector<String> [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm new to rust and just wondering if there is a shorter or more idiomatic way to convert an iterator of &str to a Vec<String>
let contents = fs::read_to_string(config.filename)?;
let la : Vec<String> = contents.lines().map( |x| String::from(x) ).collect();
I think reading the full file in a string is a bit unnecessary. Instead you can read the file line by line and collect them like that.
use std::fs::File;
use std::io::{self, BufRead};
let file = File::open(config.filename)?;
let la = io::BufReader::new(file)
.lines()
.collect::<Result<Vec<String>, _>>()?;
Nope. That is the idiomic way. You could use function pointers to shorten your code by the tiniest amount.
let la: Vec<String> = contents.lines().map(String::from).collect();

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.

Is this a grammar issue or am I understanding it wrong? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm reading the book Programming Rust. There are some places I don't quite understand.
Example 1
For this code
fn main() {
let r;
{
let x = 1;
r = &x;
}
println!("{:?}", r);
}
The author states
In this example, there are three lifetimes whose relationships we need to work out. The variables r and x each have a lifetime, extending from the point at which they’re initialized until the point that they go out of scope. The third lifetime is that of a reference type: the type of the reference we borrow to &x, and store in r.
Example 2
For this code
let v = vec![1, 2, 3];
let r = &v[1];
The author states
These rules apply in a natural way when you borrow a reference to some part of some larger data structure, like an element of a vector:
Example 3
You can “borrow a reference” to a value; references are nonowning pointers, with limited lifetimes.
Note here the quotes are in the text of the book, not added by me.
Note the bold text in the description of the two examples. Why the author always say borrow sth to sth instead of borrow sth from sth? I'm not a native English speaker and as I understand borrow sth from sth/sb is the correct usage. When talking about Rust's reference borrowing, is this usage correct?

Resources