All Questions
Tagged with option-type rust
93
questions
0
votes
2
answers
59
views
unwrap or evalulate to false in rust
I have encountered several cases similar this:
if let Some(x) = formula_chars.peek() {
if x.is_lowercase() {
todo!()
}
}
where I would like an expression to evaluate to false if an ...
2
votes
1
answer
154
views
Chain an "if let" with a boolean expression
I am trying to chain an if let condition with a regular boolean condition.
if let Some(char_first_index) = char_info.first_index_in_string && !char_info.repeats_in_string
However, when I try ...
0
votes
1
answer
79
views
How to get subslice of Options instead of Option of subslice?
From documentation, get method, given a range, returns an Option of subslice which is None if range is out of bounds:
let v = [10, 40, 30];
assert_eq!(Some(&[10, 40][..]), v.get(0..2));
assert_eq!(...
1
vote
4
answers
227
views
What is the best way to check if the value inside an option inside a result satisfies a condition in rust?
I have a function that returns something like Result<Option<Value>, Error>, and I want to check if there is a value and if it matches a condition. There are two straightforward ways:
if ...
5
votes
2
answers
139
views
How to convert Option<Option<String>> to Option<Option<&str>> in Rust?
I am trying to make this conversion (Option<Option<String>> to Option<Option<&str>>) possible, but I still failed after trying many methods including using .map.
I know ...
0
votes
1
answer
227
views
Rust Rayon collect Options into Vector
I am trying to use rayon par_iter with map (and enumerate).
However, the closure returns an Option.
Is there a way to collect the Some value of that option while filtering out Nones?
What I do ...
0
votes
1
answer
440
views
Lazy initialisation of lazy_static?
I've already had some success with lazy_static:
static ref WORD_COUNT_REPORTING_STEP_MUTEX: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
static ref INDEX_NAME: RwLock<String> = ...
4
votes
2
answers
1k
views
Is it better to return an Option<Vec<_>> or just an empty Vec<_>?
Suppose I am writing a function that takes a bunch of strings and filters out the "bad" ones.
The function then returns some strings, but there is a chance that all the strings are filtered ...
0
votes
2
answers
701
views
How to assign a value to a None Option when we use as_mut()?
For example, if we have the following struct:
struct A {
value: Option<i32>,
}
fn main() {
let mut a = A { value: Some(1) };
// want to update a's value
match a.value.as_mut() {
...
-7
votes
3
answers
739
views
Is there a concise way to check if an Option is None or if its Some contents are empty? [closed]
Is there something more concise that the below code?
let player_list: Option<Vec<Player>>;
// more code here
if player_list.is_none() || player_list.as_ref().unwrap().is_empty() {
// ...
1
vote
2
answers
328
views
Memory overhead of `Option` in Rust is not constant [duplicate]
Using the following snippet
use std::mem;
fn main() {
println!("size Option(bool): {} ({})", mem::size_of::<Option<bool>>(), mem::size_of::<bool>());
println!("...
0
votes
0
answers
22
views
How to edit structure wrapped in the Box, which is wrapped in Option? [duplicate]
I have a tree-like data structure. Every Node may have two children (left and right). Also each node may have a parent node. I need to set a parent node inside method 'set_left'. How should I do this?
...
3
votes
2
answers
175
views
How to asynchronously memoize a field of struct in an Option
Suppose I have some data Bar (e.g. database client) which I would like to create only once
but lazily for my structure Foo.
struct Bar;
struct Foo {
bar: Option<Bar>
}
To do this, I check ...
1
vote
3
answers
82
views
How to type None if it expects a function type as type argument?
Suppose I have a function that takes a callback
fn foo<T>(callback: impl FnOnce(T) -> T, value: T) -> T {
callback(value)
}
Suppose I now want to make this callback optional. The, in ...
0
votes
2
answers
110
views
What is the effect of writing the value of the option when returning the closure?
What is the effect of writing the value of the option when returning the closure?
let value = Some(0);
let closure = if value.is_some() {
|value: i32| false
} else {...