- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想获取一个链表并用结构的实例填充它,但前提是该列表尚未包含我正在考虑添加的项目。
我正在处理点,因此如果 (3,5) 在列表中我不想添加它,否则我会添加。
我当前的代码:
use std::collections::LinkedList;
struct Location {
x: i32,
y: i32,
}
fn main() {
let mut locations = LinkedList::new();
loop {
let location_set = &mut locations;
// Scanner stuff happens.
if !has_location(location_set, &next_checkpoint_x, &next_checkpoint_y) {
let point = Location {
x: next_checkpoint_x,
y: next_checkpoint_y,
};
locations.push_back(point);
}
}
}
fn has_location(location_list: LinkedList<Location>, target_x: &i32, target_y: &i32) -> bool {
true // Just until I can figure out this mutability stuff
}
我已经能够通过这些更改让它运行,但这对我来说似乎是错误的。
loop {
if !has_location(&mut locations, &next_checkpoint_x, &next_checkpoint_y) {
// stuff
}
}
fn has_location(location_list: &LinkedList<Location>, target_x: &i32, target_y: &i32) -> bool {
true
}
我不希望 has_location
能够改变链表,我只希望它能够借用它以便它可以查看它的内部。我不想考虑影响它检查的链表的 has_location
(或类似函数)。这就是我创建 location_set
的原因。我想要一些东西以只读方式引用位置,并将其传递给 has_location
函数,并且要在调用 has_location 后不销毁所引用的内容(位置)
功能。我在函数调用的参数传递中包含 &
,因为我不想传递的参数被销毁 - 所以我想借用它们?
我想要的东西是否有意义 - 如果我最初将位置声明为可变链表,我可以将它的不可变版本传递给函数进行评估吗?
最佳答案
如果您有一个可变借用,您总是可以重新借用它作为一个不可变借用。这甚至会隐式发生( &mut T
将强制转换为 &T
)。因此,您可以只传递 location_set
至 has_location
直接地。或者如果你想明确表示函数不会改变它的参数,你可以写成 &*location_set
而不是 location_set
(虽然我觉得这是不必要的)。
另外请注意,当不可变借用存在时,您不能使用可变借用;不可变借用在范围内卡住数据结构。同样,当变量在范围内有可变借用时,您不能使用该变量。在您的第一个代码示例中,您不能引用 locations
同时location_set
在范围内,因为 location_set
在 locations
上进行可变借用, 但你可以只使用 location_set
再次,因为 push_back
只需要一个可变借用(它不需要 LinkedList
按值)。
仅检查数据结构的函数通常会收到对数据结构的不可变借用。如果数据结构改为按值传递,函数将取得它的所有权并因此在返回之前销毁它(除非它被移动到别处)。因此,是的,你想要 has_location
接受对 LinkedList
的不可变借用.通过接受不可变借用(而不是可变借用),编译器将阻止您修改 LinkedList
。 (除非您使用不安全的代码)。
综合起来:
use std::collections::LinkedList;
struct Location {
x: i32,
y: i32,
}
fn main() {
let mut locations = LinkedList::new();
let next_checkpoint_x = 0;
let next_checkpoint_y = 0;
loop {
let location_set = &mut locations;
// Scanner stuff happens.
if !has_location(location_set, &next_checkpoint_x, &next_checkpoint_y) {
let point = Location { x: next_checkpoint_x, y: next_checkpoint_y };
location_set.push_back(point);
}
}
}
fn has_location(location_list: &LinkedList<Location>, target_x: &i32, target_y: &i32) -> bool {
true
}
Something I don't understand though is in your example,
location_set
is passed tohas_location
directly (so the function owns it, right?). This in my mind means that at the end ofhas_location
's scope,location_set
should be destroyed, no? How doeslocation_set
continue to exist to be used in theif
block?
不,has_location
不拥有location_set
.如果这是未实现 Copy
的任何其他类型(例如 String
),那么你是对的,但引用有特殊规则,使它们更方便使用。
当您将一个引用传递给一个函数时,编译器会自动重新借用该引用以生成一个新的引用,通常具有较短的生命周期。在这里,编译器正在重新借用可变引用并生成不可变引用;在不可变引用超出范围之前,不能使用可变引用(这里不可变引用未绑定(bind)到变量,因此您不会真正注意到这一点)。从概念上讲,就好像您将不可变引用传递给可变引用(在 Rust 中, & &mut T
不允许您改变 T
,因为该外部引用可能有多个副本),只是两个引用被“扁平化”。
Also if
location_set
is immutable, how ispush_back
able to add to the end of the list, is it because the function coerces the mutable borrow into an immutable borrow?
location_set
仍然是可变引用(因为我们使用 &mut
运算符创建它)。事实has_location
对不可变引用进行操作不会改变 location_set
的事实是可变引用。一旦调用has_location
已评估,location_set
可以作为可变引用重用,因此 push_back
等变异操作是允许的。
请记住,可变性是 Rust 纯粹的编译时概念; mut
或缺少 mut
只是让编译器验证您的代码没有执行非法操作,但是一旦您的代码被编译,这些标记就无处可见。
关于rust - 借用检查器检查可变引用的最佳实践是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42605042/
我之前让 dll 注入(inject)器变得简单,但我有 Windows 7,我用 C# 和 C++ 做了它,它工作得很好!但是现在当我在 Windows 8 中尝试相同的代码时,它似乎没有以正确的方
我正在尝试制作一个名为 core-splitter 的元素,该元素在 1.0 中已弃用,因为它在我们的项目中起着关键作用。 如果您不知道 core-splitter 的作用,我可以提供一个简短的描述。
我有几个不同的蜘蛛,想一次运行所有它们。基于 this和 this ,我可以在同一个进程中运行多个蜘蛛。但是,我不知道如何设计一个信号系统来在所有蜘蛛都完成后停止 react 器。 我试过了: cra
有没有办法在达到特定条件时停止扭曲 react 器。例如,如果一个变量被设置为某个值,那么 react 器应该停止吗? 最佳答案 理想情况下,您不会将变量设置为一个值并停止 react 器,而是调用
https://code.angularjs.org/1.0.0rc9/angular-1.0.0rc9.js 上面的链接定义了外部js文件,我不知道Angular-1.0.0rc9.js的注入(in
我正在尝试运行一个函数并将服务注入(inject)其中。我认为这可以使用 $injector 轻松完成.所以我尝试了以下(简化示例): angular.injector().invoke( [ "$q
在 google Guice 中,我可以使用函数 createInjector 创建基于多个模块的注入(inject)器。 因为我使用 GWT.create 在 GoogleGin 中实例化注入(in
我在 ASP.NET Core 1.1 解决方案中使用配置绑定(bind)。基本上,我在“ConfigureServices Startup”部分中有一些用于绑定(bind)的简单代码,如下所示: s
我在 Spring MVC 中设置 initBinder 时遇到一些问题。我有一个 ModelAttribute,它有一个有时会显示的字段。 public class Model { privat
我正在尝试通过jquery post发布knockoutjs View 模型 var $form = $('#barcodeTemplate form'); var data = ko.toJS(vm
如何为包含多态对象集合的复杂模型编写自定义模型绑定(bind)程序? 我有下一个模型结构: public class CustomAttributeValueViewModel { publi
您好,我正在尝试实现我在 this article 中找到的扩展方法对于简单的注入(inject)器,因为它不支持开箱即用的特定构造函数的注册。 根据这篇文章,我需要用一个假的委托(delegate)
你好,我想自动注册我的依赖项。 我现在拥有的是: public interface IRepository where T : class public interface IFolderReposi
我正在使用 Jasmine 测试一些 Angular.js 代码。为此,我需要一个 Angular 注入(inject)器: var injector = angular.injector(['ng'
我正在使用 Matlab 代码生成器。不可能包含代码风格指南。这就是为什么我正在寻找一个工具来“ reshape ”、重命名和重新格式化生成的代码,根据我的: 功能横幅约定 文件横幅约定 命名约定 等
这个问题在这里已经有了答案: Where and why do I have to put the "template" and "typename" keywords? (8 个答案) 关闭 8
我开发了一种工具,可以更改某些程序的外观。为此,我需要在某些进程中注入(inject)一个 dll。 现在我基本上使用这个 approach .问题通常是人们无法注入(inject) dll,因为他们
我想使用 swing、spring 和 hibernate 编写一个 java 应用程序。 我想使用数据绑定(bind)器用 bean 的值填充 gui,并且我还希望它反射(reflect) gui
我有这段代码,当两个蜘蛛完成后,程序仍在运行。 #!C:\Python27\python.exe from twisted.internet import reactor from scrapy.cr
要点是 Spring Batch (v2) 测试框架具有带有 @Autowired 注释的 JobLauncherTestUtils.setJob。我们的测试套件有多个 Job 类提供者。因为这个类不
我是一名优秀的程序员,十分优秀!