fn main() {
let data = &[1..3];
println!("Data is {:?}", data);
}
将&[1..3]
作为此声明的指定值是什么意思?
答案 0 :(得分:6)
首先:
//MainForm.cs
namespace WindowsFormsApplication
{
public partial class MainForm : Form
{
public MainForm()
{
this.Load += new EventHandler(MainForm_Load);
this.IsMdiContainer = true;
this.Size = new System.Drawing.Size(900, 600);
}
public void MainForm_Load(object sender, EventArgs e)
{
UserForm userForm1 = new UserForm();
userForm1.Text = "User Form 1";
userForm1.MdiParent = this;
userForm1.Size = new System.Drawing.Size(820, 465);
userForm1.WindowState = System.Windows.Forms.FormWindowState.Normal;
userForm1.Show();
UserForm userForm2 = new UserForm();
userForm2.TopLevel = false;
userForm2.Location = new System.Drawing.Point(200, 100);
userForm2.Size = new System.Drawing.Size(820, 465);
userForm2.Text = "User Form 2";
userForm2.Show();
userForm2.Parent = userForm1;
}
}
}
//UserForm.cs
namespace WindowsFormsApplication
{
public partial class UserForm : Form
{
public TextBox m_txtEdit;
public UserForm()
{
InitializeComponent();
}
}
}
//UserForm.Designer.cs
namespace WindowsFormsApplication
{
partial class UserForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.m_txtEdit = new System.Windows.Forms.TextBox();
this.m_txtEdit.Location = new System.Drawing.Point(5, 100);
this.m_txtEdit.Name = "m_txtEdit";
this.m_txtEdit.Size = new System.Drawing.Size(149, 20);
this.m_txtEdit.Visible = true;
this.Controls.Add(this.m_txtEdit);
}
#endregion
}
}
创建一个与let foo = &[1,2,3];
绑定具有相同生命周期的临时数组,并在foo
中存储对该数组的引用。
但是,这并不是您的计划正在做的事情。正如另一个答案所说,运行它是有用的,你得到:
foo
这看起来不像Data is [1..3]
!我们可以通过使用它的错误消息欺骗编译器告诉我们它到底是什么。我们知道它绝对不是[1,2,3]
,所以让我们先尝试一下:
()
这给我们带来了错误,其中包括:
fn main() {
let data = &[1..3];
let () = data;
}
这告诉我们答案 - error: mismatched types [--explain E0308]
--> <anon>:3:17
|>
3 |> let () = data;
|> ^^ expected &-ptr, found ()
note: expected type `&[std::ops::Range<_>; 1]`
note: found type `()`
是一个项目(data
)的数组的引用(&
),它是一个std::ops::Range<_>
对象。< / p>
答案 1 :(得分:0)
我猜您希望data
属于&[i32; 2]
类型;它不是。在这些时候,如果您不确定,可以通过尝试编写来验证它:
let data: &[i32; 2] = &[1..3];
不会起作用。错误消息将类似于:
main.rs:11:26: 11:30 error: mismatched types [E0308]
main.rs:11:26: 11:30 note: expected type `i32`
main.rs:11:26: 11:30 note: found type `std::ops::Range<_>`
你实际拥有的是:
let data: &[std::ops::Range<_>] = &[1..3];