小编Byr*_*ron的帖子

如何将计时的“ DateTime <UTC>”实例转换为“ DateTime <Local>”?

我的目标是转换utcloc

use chrono::{Local, UTC, TimeZone};

let utc = chrono::UTC::now();
let loc = chrono::Local::now();

println!("{:?}", utc);
println!("{:?}", loc);

println!("{:?}", utc.with_timezone(&Local));
println!("{:?}", Local.from_utc_datetime(&utc.naive_local()));
Run Code Online (Sandbox Code Playgroud)

...产生了以下输出:

use chrono::{Local, UTC, TimeZone};

let utc = chrono::UTC::now();
let loc = chrono::Local::now();

println!("{:?}", utc);
println!("{:?}", loc);

println!("{:?}", utc.with_timezone(&Local));
println!("{:?}", Local.from_utc_datetime(&utc.naive_local()));
Run Code Online (Sandbox Code Playgroud)

loc第二行显示的时间是我转换时想要看到的时间utc

如何将DateTime<UTC>实例正确转换为DateTime<Local>

我正在使用chrono 0.2.2。在DateTime.from_utc方法中,它甚至告诉我应该使用该TimeZone特征。但是,我缺少一些东西。

datetime rust

5
推荐指数
2
解决办法
2455
查看次数

为什么`&`需要在迭代期间构造一个元组列表?

在迭代元组列表时,&需要使其工作.这样就行了......

for &(a, b, c) in [("hello", 1.0, 5), ("world", 2.0, 2)].iter() {
    println!("{} {} {}", a, b, c);
}
Run Code Online (Sandbox Code Playgroud)

但那不会......

for (a, b, c) in [("hello", 1.0, 5), ("world", 2.0, 2)].iter() {
    println!("{} {} {}", a, b, c);
}

// type mismatch resolving `<core::slice::Iter<'_, (&str, _, _)> as core::iter::Iterator>::Item == (_, _, _)`:
// expected &-ptr,
found tuple [E0271]
Run Code Online (Sandbox Code Playgroud)

我确信它与我尚未完全内化的解构语法的复杂性有关.

你能解释一下&符背后的句法真相吗?

rust

4
推荐指数
1
解决办法
872
查看次数

使用 GitPython 库进行 git 克隆

如何使用 GitPython 库在禁用 SSL 检查的情况下进行克隆。下面的代码...

import git
x = git.Repo.clone_from('https://xxx', '/home/xxx/lala')
Run Code Online (Sandbox Code Playgroud)

...产生此错误:

Error: fatal: unable to access 'xxx': server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none
Run Code Online (Sandbox Code Playgroud)

我知道“export GIT_SSL_NO_VERIFY=1”,但是如何在 python 库中实现它?

ssl gitpython

3
推荐指数
1
解决办法
3783
查看次数

如何使用包含在`RefCell`中的`BorrowMut`?

我是BorrowMut它的忠实粉丝,因为它允许我提供允许获取参数所有权或引用的 API。这使它们更容易使用,但对我来说实施起来有点困难——这是一个可以接受的权衡,因为许多人的需求大于少数人的需求:)。

现在我想使用BorrowMutRefCell和失败,因为borrow_mut()是通过实现双方RefMut以及BorrowMut。但是,RefMut将优先,防止我深入到 中包含的实际值BorrowMut

例子

下面的代码可以让您重现该问题-目的是调用doit()Client类型:

use std::cell::RefCell;
use std::borrow::BorrowMut;

struct Hub<C> {
    client: RefCell<C>
}

impl<C> Hub<C> 
    where C: BorrowMut<Client> {

    fn new(client: C) -> Hub<C> {
        Hub {
            client: RefCell::new(client)
        }
    }

    fn builder<'a>(&'a self) -> Builder<'a, C> {
        Builder {
            hub: self
        }
    }
}

struct Builder<'a, C: 'a> {
    hub: &'a Hub<C>
}

impl<'a, C> Builder<'a, …
Run Code Online (Sandbox Code Playgroud)

rust

2
推荐指数
1
解决办法
1627
查看次数

`*(&'a mut self)`方法中的生命周期参数是否会混淆BorrowChecker?

如以下示例只要不编译'a中使用&'a mut self:

struct Foo<'a> {
    a: &'a u64,
}

impl<'a> Foo<'a> {
    fn mutate_internal(&'a mut self) {}

    fn mutate(&'a mut self) {
        self.mutate_internal();
        self.mutate_internal(); // <- This call fails the borrow-check
    }
}
Run Code Online (Sandbox Code Playgroud)

编译器使我惊讶于以下错误消息:

tests/lang.rs:1116:13: 1116:17 error: cannot borrow `*self` as mutable more than once at a time
tests/lang.rs:1116             self.mutate_internal();
                               ^~~~
tests/lang.rs:1115:13: 1115:17 note: previous borrow of `*self` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `*self` until the borrow ends …
Run Code Online (Sandbox Code Playgroud)

rust

2
推荐指数
1
解决办法
169
查看次数

一般特征实施的'继承'

我想尝试一般地实现特征,并且只要它们是兼容的,特征的用户就会自动继承这个"基础"实现.

这是测试代码我想出了(注意,fmt::Showstd::fmt::Show):

trait Outspoken {
    fn speak(&self) -> String;
}

impl<T: fmt::Show> Outspoken for T {
    fn speak(&self) -> String {
        format!("{:?}", self)
    }
}

// In theory, we can now let my-types speak
#[derive(Show)]
struct MyType(i32);

// 'Show' works
assert_eq!(format!("{:?}", MyType(15)), "MyType(15)");
// speak() however, doesn't
let mti = MyType(20);
mti.speak();
Run Code Online (Sandbox Code Playgroud)

但是,生锈并不知道这MyType是通用实现的可行候选者,因为它还没有将特性与它联系起来.上面的代码产生以下错误:

tests/lang.rs:523:9: 523:16 error: type `generics_and_traits::MyType` does not implement any method in scope named `speak` tests/lang.rs:523 mti.speak(); ^~~~~~~ tests/lang.rs:523:16: 523:16 help: methods …

rust

1
推荐指数
1
解决办法
2509
查看次数

标签 统计

rust ×5

datetime ×1

gitpython ×1

ssl ×1