在trim_right_matches我可以传递一个字符串值:
println!("{}", "[(foo)]".trim_right_matches(")]"));
// [(foo
Run Code Online (Sandbox Code Playgroud)
但是,我不能在trim_matches以下字符串中使用字符串值:
println!("{}", "[(foo)]".trim_matches("[()]"));
Run Code Online (Sandbox Code Playgroud)
因为我收到以下错误:
error[E0277]: the trait bound `std::str::pattern::StrSearcher<'_, '_>: std::str::pattern::DoubleEndedSearcher<'_>` is not satisfied
--> test.rs:2:27
|
2 | println!("{}", "[(foo)]".trim_matches("[()]"));
| ^^^^^^^^^^^^ the trait `std::str::pattern::DoubleEndedSearcher<'_>` is not implemented for `std::str::pattern::StrSearcher<'_, '_>`
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)
以下代码有效:
println!("{}", "[(foo)]".trim_matches(&['(', '[', ']', ')'] as &[_]));
// foo
Run Code Online (Sandbox Code Playgroud)
但是,它很长并且不像单个字符串值那样容易阅读; 我希望能够使用类似的字符串值trim_right_matches.
我试图改编一个WikiBooks示例,以接受csv输入和类型,但是在将传入的类型列表转换为字典并匹配用户输入方面很费劲。
// https://en.wikibooks.org/wiki/F_Sharp_Programming/Sets_and_Maps#Examples_2
module SOQN =
open System
open FSharp.Data
type Country = Country of string
type City = City of string
type CountryCapital = {
Country:Country
City:City
}
let [<Literal>] sampleCsv = @"D:\Country_Capitals.csv"
type Capitals = CsvProvider<sampleCsv, Separators=",", HasHeaders=true>
let readFromCsvFile (fileName:string) =
let data = Capitals.Load(fileName)
[ for row in data.Rows do
yield { Country = Country row.Country; City = City row.City; } ]
let countryCapitals =
readFromCsvFile sampleCsv
// -> |> Map.ofList
Console.Write("Find capital by country (type …Run Code Online (Sandbox Code Playgroud) 对于下面的条件 - 已经为a定义了模式匹配 Tuple2(BigDecimal,BigDecimal)
(r.get(0), r.get(1)) match {
case (r0: BigDecimal, r1: BigDecimal) => (bigDecimalNullToZero(r0), bigDecimalNullToZero(r1))
case (r0,r1) => {
error(s"Unable to compare [$r0] and [$r1]"); (0L,0L)
}
}
Run Code Online (Sandbox Code Playgroud)
为什么比赛没被认出?
我有一个匹配表达式,我希望可能会从中返回错误.似乎编译器无法推断最后一个案例e是一个Err(String)并且要求模式是显式的:
fn foo() -> Result<Option<u8>, String> {
unimplemented!() // don't mind the panic here
}
fn foo2() -> Result<u8, String> {
let bar = foo();
for _ in 0..3 {
let baz = match bar {
Ok(Some(b)) => b,
Ok(None) => continue,
Err(e) => return Err(e) // works
// e => return e // error[E0308]
};
}
Ok(0)
}
fn main() {
let _ = foo2();
}
Run Code Online (Sandbox Code Playgroud)
错误是:
error[E0308]: mismatched types
--> src/main.rs:13:33
|
13 | …Run Code Online (Sandbox Code Playgroud) 鉴于文档,我无法理解为什么联合上的模式匹配不能正常工作:
union A {
a1: i32,
a2: f32,
}
struct B(A);
let b = B(A { a2: 1.0 });
unsafe {
match b.0 {
A { a1 } => println!("int"),
A { a2 } => println!("float"),
}
}
Run Code Online (Sandbox Code Playgroud)
warning: unreachable pattern
--> src/main.rs:12:13
|
12 | A { a2 } => println!("float"),
| ^^^^^^^^
|
= note: #[warn(unreachable_patterns)] on by default
Run Code Online (Sandbox Code Playgroud) 在下面的代码,我想保留number_list,迭代之后,因为.into_iter()那for默认情况下使用会消耗.因此,我假设n: &i32并且我可以n通过解除引用获得价值.
fn main() {
let number_list = vec![24, 34, 100, 65];
let mut largest = number_list[0];
for n in &number_list {
if *n > largest {
largest = *n;
}
}
println!("{}", largest);
}
Run Code Online (Sandbox Code Playgroud)
有人向我透露,我们可以将其&n用作"模式":
fn main() {
let number_list = vec![24, 34, 100, 65];
let mut largest = number_list[0];
for &n in &number_list {
if n > largest {
largest = n;
}
}
println!("{}", largest); …Run Code Online (Sandbox Code Playgroud) 正如我在标题中提到的那样,我正在尝试验证用户输入的手机号码是否是印度号码。
为此,我从印度手机号码的正则表达式中引用了一种模式
但就我而言,它总是返回false。
我想验证以下内容。
app.component.html
<form class="example-form" [formGroup]="userAddressValidations">
<mat-form-field appearance="outline" class="col-sm-6">
<mat-label>10-digit Mobile Number</mat-label>
<input matInput formControlName="mobileNumber" maxlength="10" (keypress)=_keyPress($event)>
<mat-error *ngIf="userAddressValidations.get('mobileNumber').hasError('required')">
Please fill out this field.
</mat-error>
<mat-error *ngIf="userAddressValidations.get('mobileNumber').hasError('pattern')">
It is not a valid mobile number.
</mat-error>
</mat-form-field>
</form>
Run Code Online (Sandbox Code Playgroud)
app.component.ts
export class InputErrorStateMatcherExample {
userAddressValidations: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.userAddressValidations = this.formBuilder.group({
mobileNumber: ['', [Validators.required, Validators.pattern('^[6-9]\d{9}$')]]
});
}
_keyPress(event: any) {
const pattern = /[0-9]/;
let inputChar = String.fromCharCode(event.charCode);
if (!pattern.test(inputChar)) { …Run Code Online (Sandbox Code Playgroud) 我想在Haskell中编写一个函数,它将第二个参数给出的列表旋转第一个参数指示的位置数.使用模式匹配,实现递归函数
我写了以下函数:
rotate :: Int -> [a] -> [a]
rotate 0 [y]= [y]
rotate x [y]= rotate((x-1) [tail [y] ++ head [y]])
Run Code Online (Sandbox Code Playgroud)
但此功能总是会产生错误.有什么办法可以解决吗?该函数在运行时应执行以下操作:
rotate 1 "abcdef"
"bcdefa"
Run Code Online (Sandbox Code Playgroud) 我有很多行的整数,每行有7列,它是从实验记录的一些生物点.数字仅为1到7,我想确定出现的那些整数的常见模式.
first few rows of df:
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 1 2 3 4 6 7 7
[2,] 1 2 2 3 3 5 7
[3,] 1 2 2 3 3 4 5
[4,] 2 3 4 7 7 7 7
[5,] 1 1 3 4 5 6 7
[6,] 2 2 3 3 4 6 6
[7,] 1 1 2 3 3 6 6
[8,] 2 2 3 4 6 6 7
...
Run Code Online (Sandbox Code Playgroud)
为实例,
desired …Run Code Online (Sandbox Code Playgroud) r frequency pattern-matching frequency-distribution frequency-analysis
product' :: [Integer] -> Integer
product' (x:xs)
| (x:xs) == [] = 1
| otherwise = x * (product' (xs))
Run Code Online (Sandbox Code Playgroud)
第一个不起作用并给出错误.下面的一个是使用模式匹配技术,并正常工作.
productP :: [Integer] -> Integer
productP [] = 1
productP (x:xs) = x * (productP (xs))
Run Code Online (Sandbox Code Playgroud)