MVCE:
use std::collections::HashMap;
use std::fmt;
use std::marker::PhantomData;
use std::str::FromStr;
use serde; // 1.0.85
use serde::de::{self, MapAccess, Visitor}; // 1.0.85
use serde_derive::Deserialize; // 1.0.85
use toml; // 0.4.10
use void::Void; // 1.0.2
// See: https://serde.rs/string-or-struct.html
fn string_or_struct<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: serde::Deserialize<'de> + FromStr<Err = Void>,
D: serde::Deserializer<'de>,
{
// This is a Visitor that forwards string types to T's `FromStr` impl and
// forwards map types to T's `Deserialize` impl. The `PhantomData` is to
// …Run Code Online (Sandbox Code Playgroud) 我正在使用 Rust git2 crate 来克隆这样的 Git 存储库
use git2::Repository;
fn main() {
let repo = Repository::clone(
"https://github.com/rossmacarthur/dotfiles",
"dotfiles"
).expect("failed to clone repository");
repo.checkout("mybranch"); // need something like this.
}
Run Code Online (Sandbox Code Playgroud)
我希望能够检出分支、提交或标签。
我查看了以下文档,但仍然不确定使用哪种方法
我可以执行以下操作,但它只会更改文件
let object = repo
.revparse_single("mybranch")
.expect("failed to find identifier");
repo.checkout_tree(&object, None)
.expect(&format!("failed to checkout '{:?}'", object));
Run Code Online (Sandbox Code Playgroud)
如果我进行重置,它会更改 HEAD 而不是当前分支
repo.reset(&object, git2::ResetType::Soft, None)
.expect(&format!("failed to checkout '{:?}'", object));
Run Code Online (Sandbox Code Playgroud) 这是一个令人讨厌的例子:
// Some traits
trait Behaviour {
type Sub: SubBehaviour;
}
trait SubBehaviour {}
// Some implementations of these traits
struct A;
impl Behaviour for A {
type Sub = B;
}
struct B;
impl SubBehaviour for B {}
// Struct that holds a collection of these traits.
struct Example<'a> {
behaviours: Vec<&'a Behaviour>,
}
impl<'a> Example<'a> {
fn add_behaviour<T: Behaviour>(&mut self, b: &'a T) {
self.behaviours.push(b);
}
}
fn main() {
let b = A;
let mut e = …Run Code Online (Sandbox Code Playgroud) 我是Haskell的新手,我希望能够使用newtype,因此我可以告诉它是什么,但我也必须从字符串中读取它.我有
newtype SpecialId Int
deriving (Eq, Ord, Show)
Run Code Online (Sandbox Code Playgroud)
我想能够read "5" :: SpecialId如果我在新类型中导出读取它不起作用它只能工作read "SpecialId 5" :: SpecialId.我试过了
instance Read SpecialId where
readsPrec _ s = read s
Run Code Online (Sandbox Code Playgroud)
但这给了我
SpecialId *** Exception: Prelude.read: no parse
Run Code Online (Sandbox Code Playgroud) 我有一个输出一堆东西的命令,例如运行mycmd会给:
foobar
derp derp
like so
etc
Run Code Online (Sandbox Code Playgroud)
其中一些行将包含空格.
如何将这些读入zsh中的数组,以便${arr[1]}给出foobar,${arr[2]}给出derp derp等等.
我尝试了类似的东西,但它似乎将数组拆分为字符而不是换行符.
IFS=$'\n' read -d '' -r arr <<< "$(mycmd)"
Run Code Online (Sandbox Code Playgroud)
即${arr[1]}给予f应该给予的时间foobar
说我有这样的事情
class Circle c where
x :: c -> Float
y :: c -> Float
radius :: c -> Float
data Location = Location { locationX :: Float
, locationY :: Float
} deriving (Show, Eq)
data Blob = Location { blobX :: Float
, blobY :: Float
, blobRadius :: Float,
, blobRating :: Int
} deriving (Show, Eq)
instance Circle Location where
x = locationX
y = locationY
radius = pure 0
instance Circle Blob where
x = …Run Code Online (Sandbox Code Playgroud) 所以我知道我可以用这样的字符串解析一些东西s:
read s :: Int
Run Code Online (Sandbox Code Playgroud)
但这仅在字符串是您正在阅读的整个类型时才有效.我实际上希望能够读取相同类型的流并创建一个列表但是我找不到任何类型的
Read a => String -> (a, String)
Run Code Online (Sandbox Code Playgroud)
或者类似的东西.基本上我希望能够readSeries "5 6 7"得到类似和得到的东西[5, 6, 7],我认为最简单的是如果我能得到一些东西来返回字符串的其余部分.