从标准库:
两个指针必须派生自指向同一对象的指针。(请参见下面的示例。)
Run Code Online (Sandbox Code Playgroud)let ptr1 = Box::into_raw(Box::new(0u8)); let ptr2 = Box::into_raw(Box::new(1u8)); let diff = (ptr2 as isize).wrapping_sub(ptr1 as isize); // Make ptr2_other an "alias" of ptr2, but derived from ptr1. let ptr2_other = (ptr1 as *mut u8).wrapping_offset(diff); assert_eq!(ptr2 as usize, ptr2_other as usize); // Since ptr2_other and ptr2 are derived from pointers to different // objects, computing their offset is undefined behavior, even though // they point to the same address! unsafe { let zero = ptr2_other.offset_from(ptr2); // Undefined …
如果我有一长串必须在很多地方重复的类型界限,我该如何给它们命名?
例如,如果不是:
fn foo<T: Thing>(t: T) -> T
where T: Copy, T: Debug { ... }
Run Code Online (Sandbox Code Playgroud)
我想写:
fn foo<T: Thing>(t: T) -> T
where T: CopyDebug { ... }
Run Code Online (Sandbox Code Playgroud)
在哪里CopyDebug定义为Copy+Debug?
我正在尝试在https://rustgym.com/leetcode/98中编译 LeetCode 问题 98 的 rust 代码
,但是,我在这一行中收到错误let node = node.borrow();::
type annotations needed for `&Borrowed`
type must be known at this point
rustcE0282
s0098_validate_binary_search_tree.rs(66, 17): consider giving `node` the explicit type `&Borrowed`, where the type parameter `Borrowed` is specified
Run Code Online (Sandbox Code Playgroud)
不过Leetcode编译是没有问题的。这是代码。
use std::rc::Rc;
use std::cell::RefCell;
type TreeLink = Option<Rc<RefCell<TreeNode>>>;
trait Inorder {
fn inorder(&self, visit: &mut dyn FnMut(i32));
}
impl Inorder for TreeLink {
fn inorder(&self, visit: &mut dyn FnMut(i32)) {
if let Some(node) = self { …Run Code Online (Sandbox Code Playgroud) 我有一个元组向量:
let v = vec![(1, 1), (1, 1), (1, 3), (1, 4), (2, 2), (2, 4), (2, 6)];
Run Code Online (Sandbox Code Playgroud)
我想把它分成两个列表。第一个列表包含每个元组中的第一个元素,第二个列表包含第二个元素,即:
l1 = [1, 1, 1, 1, 2, 2]和l2 = [1, 1, 3, 4, 2, 4]。
我怎样才能做到这一点?
在 Haskell 编程之后,我即将开始学习 Rust。我对关键字trait感兴趣,但我注意到您只能引用一种类型(Self)。
在 Haskell 中有一个针对这种行为的编译指示:
{-# LANGUAGE MultiParamTypeClasses #-}
class ExampleBehaviour a b where
combine :: a -> a -> b
co_combine :: b -> b -> a
Run Code Online (Sandbox Code Playgroud)
然而,我看不到在 Rust 中有机地实现这种行为的方法。
我一直在尝试查找有关 Rust 如何在涉及模块边界的情况下解决特征包实现的文档,但没有找到与之直接相关的内容。
让我们考虑一个包含两个相似但略有不同的代码片段的示例:
第一个代码片段编译得很好,但会遭受无限的运行时递归。ClonableIterator发生这种情况是因为for 的全面实现T: Iterator+Clone与 which is 相匹配Box<dyn ClonableIterator<'a>>,这要归功于我们手动实现Clone和nowBox的全面实现。IteratorIterator+Clone
//the trait itself, with the indirection to call box_clone on the base type
trait ClonableIterator<'a>: Iterator<Item = &'a u32> + 'a {
fn box_clone(&self) -> Box<dyn ClonableIterator<'a>>;
}
//The blanket implementation of the trait for all clonable iterators
impl<'a, T: Iterator<Item = &'a u32> + Clone + 'a> ClonableIterator<'a> for T {
fn box_clone(&self) -> Box<dyn ClonableIterator<'a>> …Run Code Online (Sandbox Code Playgroud) 我正在尝试让柴油箱与 SQLite 一起使用,但是脱离了入门指南,它似乎不适用于 sqlite。
该代码适用于 postgres 但不适用于 sqlite
diesel::insert_into(schema::subscriptions::table)
.values(&new_subscription)
.get_result(&connection)
.expect("Error saving new subscription")
Run Code Online (Sandbox Code Playgroud)
错误
error[E0277]: the trait bound `Sqlite: SupportsReturningClause` is not satisfied
--> src/responder.rs:41:12
|
41 | .get_result(&connection)
| ^^^^^^^^^^ the trait `SupportsReturningClause` is not implemented for `Sqlite`
Run Code Online (Sandbox Code Playgroud)
我可以在文档中看到一些有关柴油退货条款的参考资料,但我不完全确定应该将其更改为什么才能使其正常工作。
我有一个向量[(5, 1), (9, 1), (4, 2)]
我希望它被排序为[(4, 2), (9, 1), (5, 1)]
每隔一个元素然后第一个元素按降序排序。可以使用sort_by(|x, y| y.cmp(x))函数吗?
我想知道作为参数的函数之间是否有任何区别(在优化方面):
fn thing(a: &Option<T>)
Run Code Online (Sandbox Code Playgroud)
与以参数为参数的函数相比:
fn thing(a: Option<&T>)
Run Code Online (Sandbox Code Playgroud) 假设我有以下工作代码:
use std::collections::VecDeque;
fn modify<S, F>(state: &mut S, func: F)
where
F: for<'a> Fn(&'a mut S) -> Box<dyn Iterator<Item = &mut u64> + 'a>
{
let mut prev = 1;
for _ in 0..3 {
for item in func(state) {
let val = *item;
*item = val * prev;
prev = val;
}
}
}
fn main() {
let mut state: VecDeque<u64> = vec![1,2,3,4].into();
modify(&mut state, |s| Box::new(s.iter_mut()));
assert_eq!(state, [48, 8, 24, 864]);
modify(&mut state, |s| Box::new(s.iter_mut().take(2)));
assert_eq!(state, [147456, 7077888, …Run Code Online (Sandbox Code Playgroud)