How to use a crate with a hyphen (-) in the name? [duplicate] - rust

This question already has answers here:
crate name with hyphens not being recognized
(2 answers)
Why is changing hyphenated crate names to underscored names possible and what are the rules for naming under such ambiguous scenarios?
(1 answer)
Closed 1 year ago.
This crate's name is:
name = "rtsp-types"
How can I use it on my project?
I added to the Cargo.toml:
[dependencies]
rtsp-types = { path = "../rtsp-types" }
however I can't do
use rtsp-types
because of the - symbol.

Related

Using String::from with variables [duplicate]

This question already has answers here:
How to create a formatted String out of a literal in Rust?
(2 answers)
Closed 4 months ago.
Is there a way to use String::from with variables? I tried using it similarly to println!(), but doesn't seem to work.
String::from("[INFO]: {}", message)
Expected:
// Returns: "[INFO]: My info message here"
Got:
// Error: argument unexpected
Rust doesn’t really practice variadic functions because we have big metaprogramming potential: modern generic system and about 6 types of macros.
In this case you need the format macro:
format!("[INFO]: {}", message)

Python re.sub to capture groups unexpected behaviour [duplicate]

This question already has answers here:
Regular expression to stop at first match
(9 answers)
Python non-greedy regexes
(7 answers)
Closed 2 years ago.
Consider following code:
import re
mystring = "\enquote {aaa} a \enquote {bbb}"
text = re.sub(r"\\enquote \{(.+)\}", r"\1", mystring)
print(text)
is outputing:
"aaa} a \enquote {bbb"
but I am acrually trying to achieve output of:
"aaa a bbb"
What have I misunderstood?
Background: I am trying to do some simple conversion from LaTeX format to general text, for which I need to replace LaTeX commands, but retain raw text itself (and sometimes do also some actual replacing). So how can I do group capture in python?

Python, what is this for loop doing? [duplicate]

This question already has answers here:
What does "list comprehension" and similar mean? How does it work and how can I use it?
(5 answers)
Closed 3 years ago.
I have scoured the internet and I cannot find any reference to this type of for loop:
variable = [(item["attribute1"], item["attribute2]") for item in piece_of_json_data]
I am using this to update wtform's SelecField choices:
form.SelectField.choices = variable
but I can only get it to work if I replace one of the attributes in parenthesis with a static number:
variable = [(1, item["attribute2"]) for item in piece_of_json_data]
but that sets the value of the option field to "1", when I need the option values to be one of the attributes as a string.
Does this create a dict? a tuple? is there some kind of terminology for this that I can use to find documentation?
Thanks to the comments, I now understand that I am using list comprehension to create a tuple. The tuple works fine with both string and integer values. My issue has to do with .choices not accepting a string.
I found that my only problem is that I had coerce set to int on my selectfield, so naturally it wanted an integer.

The FromStr trait [duplicate]

This question already has answers here:
Using functions defined in primitive modules
(1 answer)
no function or associated item named `from_str` found for type `hyper::mime::Mime`
(1 answer)
How to create an `IpAddr` without knowing the specific IP version?
(1 answer)
Closed 3 years ago.
I'm trying to use the Cidr crate, and need to convert strings to CIDRs. But it's not working and I can't figure out why. I think I need to use something, but std::str::FromStr doesn't help. What's worse is that I don't even know where to look next.
error[E0599]: no variant named `from_string` found for type `cidr::AnyIpCidr` in the current scope
--> src/main.rs:45:30
|
45 | let c = cidr::AnyIpCidr::from_string(ipstr);
| -----------------^^^^^^^^^^^
| |
| variant not found in `cidr::AnyIpCidr`

Underscore "_" in for loop [duplicate]

This question already has answers here:
What is the purpose of the single underscore "_" variable in Python?
(5 answers)
Trying to understand Python loop using underscore and input
(4 answers)
Closed 4 years ago.
I was check a solution on hacker rank where i was solving a question asking to print the name of the person with the second highest score from an input which has to be converted to a nested list first .
I understood all the logic in the code and most part of the code but why the Underscore ( _ ) in the for loop .Please explain me the code if there is a different concept .
marksheet = []
for _ in range(0,int(input())):
marksheet.append([input(), float(input())])
second_highest = sorted(list(set([marks for name, marks in marksheet])))[1]
print('\n'.join([a for a,b in sorted(marksheet) if b == second_highest]))
It's a Pythonic convention to use underscore as a variable name when the returning value from a function, a generator or a tuple is meant to be discarded.
In your example, the code inside the for loop does not make any use of the values generated by range(0,int(input())), so using an underscore makes sense as it makes it obvious that the loop does not intend to make use of it.

Resources