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
I have a string, which refers to Team Foundation and is being used in a few functions. However, I would like it to check whether that file (TF.exe) is located on Enterprise (const TF: &'static str = "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/Common7/IDE/CommonExtensions/Microsoft/TeamFoundation/Team Explorer/TF.exe";) or Professional (.../2019/Professional/...). I've read about solution here, however, it is not the best one. Any ideas? Thanks in advance.
You can use std::path::Path::starts_with like so:
use std::path::Path;
const TF: &'static str = "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/Common7/IDE/CommonExtensions/Microsoft/TeamFoundation/Team Explorer/TF.exe";
const ENTERPRISE: &'static str = "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise";
const PROFESSIONAL: &'static str = "C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional";
fn main() {
let path = Path::new(TF);
if path.starts_with(ENTERPRISE) {
println!("{}", "enterprise")
} else if path.starts_with(PROFESSIONAL) {
println!("{}", "professional")
}
}
If the string is not user-writable, you could use
let professional = str.contains("es");
,or any other string or substring that would only be found in the human readable word Enterprise or Professional, and nowhere else in the location, which would save a lot of memory since a lot of big strings for the starts_with checks could be ignored, but #Ekrim Dinçel 's answer is probably best in most circumstances
Related
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 6 months ago.
Improve this question
I'm trying to read a gzip-compressed file line by line.
I used the method suggested in this post.
It works fine for the first ~700 lines of the file, but then stops without error and ignores the next millions of lines.
Here is a minimal working example (Rust 1.57.0):
use std::io::{prelude::*, BufReader};
use std::fs::File;
use flate2; // 1.0
use flate2::read::GzDecoder;
fn main() {
let r1 = "/home/path/to/bigfile.gz";
let file = File::open(r1).unwrap();
let reader = BufReader::new(GzDecoder::new(file));
let mut i = 0;
for l in reader.lines() {
println!("{}", i);
i+=1;
}
}
Since this code compiles and is able to read the start of the file, why does it stop at some point?
I found the issue, my files where not gzip encoded but bgzip encoded, meaning the flate2 parser thought the end of one bgzip block was the end of the file.
The solution is to use rust_htslib::bgzf::Reader like this :
let r1_reader = BufReader::new(Reader::from_path(r1).unwrap());
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
[{"features":[{"$":{"name":"Default test","durationms":"73546"}}]}]
Getting an output like this while converting an xml to json.
Want an output without $ like this:
[{"features":[{"name":"Default test","durationms":"73546"}]}]
Can anyone help me?
Thanks in advance!
And i used 'xml2js'.
You can use array map method in order to format the data in the expected format
let data = [{"features":[{"$":{"name":"Default test","durationms":"73546"}}]}]
//You can loop through the array
let result = data.map(d => ({
...d,
//Loop through features array and return only the value of "$"
features: d.features.map(f => f.$)
}))
console.log(result)
Hope this helps.
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
I am doing an exercise in the Eloquent Javascript book and am having trouble getting access to a variable with JSON information in an adjacent .js file.
My file structure looks like this: eloquentJs (folder)> ancestry.js, chapter5json.js
I am including a require statement at the top of my chapter5json.js file:
require("./ancestry.js");
as well as:
module.exports = ANCESTRY_FILE;
at the bottom of my ancestry.js file.
When I try to run the following code in chapter5json.js:
var ancestry = JSON.parse(ANCESTRY_FILE);
console.log(ancestry.length);
I get an error that the
variable ANCESTRY_FILE is not defined.
Does anyone see what I'm doing wrong here?
You need to store the require statement like this: let ancestor = require("./ancestry.js")
Since the file is simply a json, simply use the ancestor like this:
let ancestry = JSON.parse(ancestor);
Now you can use all the variables using the reference of ancestry
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 4 years ago.
Improve this question
I have a line example "one two free" and I need programatically to convert this to r#"one two free"# via macros, is this possible?
#[macro_export]
macro_rules! fmt_wrap {
($msg:block) => {
}
}
fn main() {
println!(fmt_wrap!("one two free"));
}
You can't.
The reason is quite simple: macros by example (the things you declare with macro_rules!) work on a token stream, not a stream of characters. The input to a macro invocation has to be a valid token stream. This means that the compiler has to tokenize the code before expanding any macros. But the difference between normal string literals "foo" and raw string literals r#"foo"# is only during tokenization! A string literal is one token.
This means that this:
fmt_wrap!("foo " bar");
Will never work. Before expanding fmt_wrap, the compiler has to convert its input into a valid token stream. But that's not possible!
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 6 years ago.
Improve this question
I am looking for some library which take string as input and result boolean based on, if input string is present in english dictionary
For mac user default english dictionary can be found at location /usr/share/dict/web2
Hence we can,
>> var dict = scala.io.Source.fromFile("/usr/share/dict/web2").getLines.toSet
>> dict.contains("apple")
result : true
>> dict.contains("appl")
result : false