Build Option<Vec<String>> from Iterator<Item = String> [closed] - rust

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have the following logic implemented:
let assigned_courses: Option<Vec<String>> =
schools.courses.iter().map(|c| c.to_string()).collect();
Since this is an optional variable, I get the error:
value of type `std::option::Option<Vec<std::string::String>>` cannot
be built from `std::iter::Iterator<Item=std::string::String>`
How do I handle this issue? If it is not optional, it does not throw this error.

Why not?
let assigned_courses: Vec<String> =
schools.courses.iter().map(ToString::to_string).collect();
Or if you really need an Option for later usage within your context
let assigned_courses: Option<Vec<String>> =
Some(schools.courses.iter().map(ToString::to_string).collect());

Related

Example from Rust in Action does not work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
Improve this question
I am following the textbook Rust in Action and trying to write the example on page 71:
When I try to define an args like:
let args = App::new("chapter1")
.about("Searches for patterns!").arg(Arg::with_name("pattern"))
.help("The pattern to look for!")
.takes_value(true)
.required(true).get_matches();
I get an error,
no method named `takes_value` found for struct `App` in the current scope
items from traits can only be used if the trait is in scoperustcE0599
any_arg.rs(37, 8): the method is available for `App<'_, '_>` here
main.rs(1, 1): the following trait is implemented but not in scope; perhaps add a `use` for it:: `use clap::args::any_arg::AnyArg;
Can anyone tell me what the problem is? Because it seems to work in the textbook?
What if you try instead (sorry, didn't test it):
let args = App::new("chapter1")
.about("Searches for patterns!").arg(Arg::with_name("pattern")
.help("The pattern to look for!")
.takes_value(true))
.required(true).get_matches();
or
let args = App::new("chapter1")
.about("Searches for patterns!").arg(Arg::with_name("pattern")
.help("The pattern to look for!")
.takes_value(true)
.required(true))
.get_matches();
takes_value() in clap (version 3.*) is a method from the struct Arg, not App. The parentheses you show imply that it was referring to the struct App.

How to pass an object as a parameter to another function in Rust? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 11 months ago.
Improve this question
This is my code:
use handlebars::{Handlebars};
use serde_json::json;
use std::error::Error;
use std::fs;
use std::io::{BufReader, Read};
use std::path::Path;
use std::fs::File;
pub const base_path: &str = "/Users/myname/Desktop/projectname/";
fn main() -> Result<(), Box<dyn Error>>{
let mut reg = Handlebars::new();
// problem zone: how to pass the reg object here?
//register_template_files(reg);
Ok(())
}
// using registry object
fn register_template_files(registry: registry) {
//using object of type Registry to register Templates
}
I don't know Rust good enough to know how to pass the reg object to another function. How to do this?
Instead of registry: registry, you need to use registry: Registry and import that type from handlebars.
You probably also want to use references registry: &Registry, because what looks like pass-by-value instead is the notation for move semantics in Rust, which basically means that any value you pass into a function is thereafter no longer valid and trying to do that will provoke errors from the compiler.

Why ES6 (7/8...) is able to be compatible with ES5(3) since there is so much difference in grammar? [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
Addition: Why Python3 choose to design new features at cost of compatibility? Is there anything to do with the nature of language?
Why ES6 (7/8…) is able to be compatible with ES5(3) since there is so much difference in grammar?
There aren't differences so much as additions. When new grammar/syntax is added in JavaScript, it's defined carefully so that it would have been a syntax error in earlier versions. That's important, because maintaining backward compatibility with the mind-bogglingly huge amount of JavaScript code out in the wild is an important goal for the committee that moves JavaScript forward (ECMA's TC39). (Backward compatibility isn't 100%, but it's about 99.9999%.) But if something new would have been a syntax error before, then there isn't any working code out there that has the new thing.
For example: Adding async functions. In JavaScript from the beginning, a function declaration looks like this:
function example() {
// ...
}
To create async functions, it became possible to put async in front of function:
async function example() {
// ...
}
That's new grammar. It only works because before it was added, that would have been a syntax error, the same one you get if you do this today:
blah function example() {]
// ...
}
Similarly, with parameter default values, you specify the default value with an = and an expression in the parameter list:
function example(param = "default") {
// ...
}
That was a syntax error until it was added. So were arrow functions, generator functions, class syntax, nullish coalescing, optional chaining, destructuring, and other new grammar/syntax.
So since these would have been syntax errors, they don't exist in working code out in the wild.

Webstorm nodejs conventions spaces [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 8 years ago.
Improve this question
I read that the convention of node js of spaces is double spaces, but by default it is 4 spaces, how to configure it?
It is:
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
should be this way:
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
That's not a "convention" - that's called code style; and more specifically, it looks like it's the NPM Coding Style, as it's followed by lots of Node developers out there.
You can customize this easily by going to Settings -> Code Style -> JavaScript in WebStorm.
P.S.: I personally like a lot the jQuery Code Style - it's more readable than any other.

Simultaneous access http filter [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 8 years ago.
Improve this question
I implemented a Http filter using Filter interface and it works fine in localhost.
The problem is that in a testing environment when two users want to access to the application this filter does not work like always. It mixes the data between two users. I know it because I have lots of logs reporting me the steps every moment. I don't know if there is any problem with the simultaneous access.
Like servlets, filters are application scoped. There's only one instance of a filter class being created during application startup and the very same instance is being reused across all HTTP requests throughout the application's lifetime. Your problem symptoms indicate that you're for some reason assigning request or session scoped variables as an instance variable of the filter instance, such as request parameters or session attributes, or perhaps even the whole request or session object itself.
For example,
public class MyFilter implements Filter {
private String someParam;
public void doFilter(... ...) {
someParam = request.getParameter("someParam");
// ...
}
}
This is not threadsafe! You should be declaring them in the method local scope:
public class MyFilter implements Filter {
public void doFilter(... ...) {
String someParam = request.getParameter("someParam");
// ...
}
}
See also:
How do servlets work? Instantiation, sessions, shared variables and multithreading

Resources