如何为我的结构实现 FromIterator 特性?
How do I implement a FromIterator trait for my struct?
use itertools::Itertools;
struct Runner {
h: u8,
m: u8,
s: u8,
}
impl Runner {
fn from(v: (u8,u8,u8)) -> Runner {
Runner {
h: v.0,
m: v.1,
s: v.2,
}
}
}
fn string_to_vec(strg: &str) -> Vec<Runner> {
strg.split(", ")
.map(|personal_result| personal_result.split('|'))
.flatten()
.map(|x| x.parse::<u8>().unwrap())
.tuples::<(_, _, _)>()
.collect::<Vec<Runner>>()
}
fn stati(strg: &str) -> String {
let run_vec = string_to_vec(strg);
}
所以我正在尝试将元组收集到我的结构的 Vec 中。不知道该怎么做
https://www.codewars.com/kata/55b3425df71c1201a800009c/train/rust
你的 from()
函数不会被神奇地调用,即使你实现了特征 From
,你也需要明确地调用它:
use itertools::Itertools;
struct Runner {
h: u8,
m: u8,
s: u8,
}
impl From<(u8, u8, u8)> for Runner {
fn from(v: (u8, u8, u8)) -> Runner {
Runner {
h: v.0,
m: v.1,
s: v.2,
}
}
}
fn parse_runner(strg: &str) -> Vec<Runner> {
strg.split(", ")
.flat_map(|personal_result| personal_result.split('|'))
.map(|x| x.parse::<u8>().unwrap())
.tuples::<(_, _, _)>()
.map(From::from)
.collect::<Vec<Runner>>()
}
use itertools::Itertools;
struct Runner {
h: u8,
m: u8,
s: u8,
}
impl Runner {
fn from(v: (u8,u8,u8)) -> Runner {
Runner {
h: v.0,
m: v.1,
s: v.2,
}
}
}
fn string_to_vec(strg: &str) -> Vec<Runner> {
strg.split(", ")
.map(|personal_result| personal_result.split('|'))
.flatten()
.map(|x| x.parse::<u8>().unwrap())
.tuples::<(_, _, _)>()
.collect::<Vec<Runner>>()
}
fn stati(strg: &str) -> String {
let run_vec = string_to_vec(strg);
}
所以我正在尝试将元组收集到我的结构的 Vec 中。不知道该怎么做 https://www.codewars.com/kata/55b3425df71c1201a800009c/train/rust
你的 from()
函数不会被神奇地调用,即使你实现了特征 From
,你也需要明确地调用它:
use itertools::Itertools;
struct Runner {
h: u8,
m: u8,
s: u8,
}
impl From<(u8, u8, u8)> for Runner {
fn from(v: (u8, u8, u8)) -> Runner {
Runner {
h: v.0,
m: v.1,
s: v.2,
}
}
}
fn parse_runner(strg: &str) -> Vec<Runner> {
strg.split(", ")
.flat_map(|personal_result| personal_result.split('|'))
.map(|x| x.parse::<u8>().unwrap())
.tuples::<(_, _, _)>()
.map(From::from)
.collect::<Vec<Runner>>()
}