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());
Related
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 6 months ago.
Improve this question
The goal of this code is to alphabetize and uppercase the inputted tuple of values. However, it is returning none when I run it. I find this odd since I have a return and I beilieve everything is in correct order. If you can help find the answer, thanks. Here is the code:
def sorter(*args):
args = " ".join(args)
uppercased = args.upper()
listed = list(uppercased)
sorted1 = listed.sort()
return sorted1
print(sorter('happy', 'apple', 'zain', 'freindly', 'jakob'))
Run your code in the Python Tutor Visualizer and step through it line by line, and you will see that listed.sort() doesn't return anything but instead mutates listed:
Before executing listed.sort():
After executing listed.sort():
The docs for list.sort also tell you that the list is sorted in-place, and the function signature doesn't have a return value.
The solution is therefore to use listed after sorting it, instead of creating a new variable sorted1.
(Note that there are other logical mistakes in your code which will prevent it from delivering the result you probably expected, even after this specific issue is fixed, but that's beyond the scope of this question and answer.)
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
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!
This question already has answers here:
How to disable unused code warnings in Rust?
(11 answers)
How to quiet a warning for a single statement in Rust?
(2 answers)
Closed 4 years ago.
I have lines that return Result. The result does not matter and I don't want unwrap() or any logging. (The frequent failures are expected)
How can I silence the unused warnings for only these lines?
#[allow(unused_must_use)] seems to work when applied to fn level, but this does not work:
#[allow(unused_must_use)]
std::fs::remove_file(&path1);
#[allow(unused_must_use)]
std::fs::remove_file(&path2);
Edit:
while let _ = .. works as a workaround, #[allow(unused..)] specifically exists for this case, and the compiler also suggests to use it. Reading let _ = .. adds another layer of thinking. (assign and then abandon) So I prefer #[allow(..)] although it is more verbose. (If I see let _ = .. frequent enough and I get used to it, I may change my preference)
So I searched around and found some codes that apply macros to the statement level.
This question asks for why #[allow(unused_must_use)] for a line doesn't work-- wrong syntax? bug? just unimplemented yet for unused_must_use?
Edit:
According to this:
How to quiet a warning for a single statement in Rust?
My code should work.
Reading these:
https://github.com/rust-lang/rust/issues/36675
https://github.com/rust-lang/rust/issues/15701
I tried the allow on the scope level and it worked.
#[allow(unused_must_use)] {
std::fs::remove_file(&path1);
std::fs::remove_file(&path2);
}