Why do I get the error E0277: the size for values of type `[integer]` cannot be known at compilation time? [duplicate]
This question already has an answer here:
What does “Sized is not implemented” mean?
2 answers
What is the difference between a slice and an array?
2 answers
How can I fix the error E0277: the trait bound `[usize]: std::marker::Sized` is not satisfied?
2 answers
In the following code I am getting the error:
error[E0277]: the size for values of type `[integer]` cannot be
known at compilation time at the line `for n in numbers[1..] {`
I did some searching around, but found nothing.
fn main()
let mut numbers = Vec::new();
numbers.push(1);
numbers.push(32);
numbers.push(43);
numbers.push(42);
// ... And many more
println!(":?", numbers); // Sanity
let mut sum = 0;
// Problem code area
for n in numbers[1..]
sum = sum + n;
// Problem code area
println!("", sum);
Also, the problem lines work if I replace them with following (adding &
and *
for ownership/borrowing and dereferencing)
for n in &numbers[1..]
sum = sum + *n;
Why is the compilation failing in the former way?
rust
marked as duplicate by hellow, Shepmaster
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 10 at 14:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What does “Sized is not implemented” mean?
2 answers
What is the difference between a slice and an array?
2 answers
How can I fix the error E0277: the trait bound `[usize]: std::marker::Sized` is not satisfied?
2 answers
In the following code I am getting the error:
error[E0277]: the size for values of type `[integer]` cannot be
known at compilation time at the line `for n in numbers[1..] {`
I did some searching around, but found nothing.
fn main()
let mut numbers = Vec::new();
numbers.push(1);
numbers.push(32);
numbers.push(43);
numbers.push(42);
// ... And many more
println!(":?", numbers); // Sanity
let mut sum = 0;
// Problem code area
for n in numbers[1..]
sum = sum + n;
// Problem code area
println!("", sum);
Also, the problem lines work if I replace them with following (adding &
and *
for ownership/borrowing and dereferencing)
for n in &numbers[1..]
sum = sum + *n;
Why is the compilation failing in the former way?
rust
marked as duplicate by hellow, Shepmaster
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 10 at 14:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I don't get your question, because you already came up with an solution. What is your concern?
– hellow
Nov 10 at 7:30
Also @hellow, the error message you posted is different. It's for the same error code, but not related. The message posted above is full message. Did you try running the code? You can do it here: play.rust-lang.org
– Sid
Nov 10 at 10:02
Please don't accuse anyone on downvoting or voting for close your question. First, I did not downvote it, second it you should look at the full error message! As I posted (it's gone?!) the error message is telling you what to do.
– hellow
Nov 10 at 14:05
2
Sid, when you ask a question and there are some comments, e.g. recommendations or questions regarding the question, then we ask them. When we think, that the question already has been answered, we mark it as an duplicate. It doesn't mean, that it is necessarily a bad question, just that it has been answered. We are happy to help you, when your questions are good, which means they are clear and have a good mcve (when it is about a programming problem). Please read the info page for rust.
– hellow
Nov 10 at 15:13
add a comment |
This question already has an answer here:
What does “Sized is not implemented” mean?
2 answers
What is the difference between a slice and an array?
2 answers
How can I fix the error E0277: the trait bound `[usize]: std::marker::Sized` is not satisfied?
2 answers
In the following code I am getting the error:
error[E0277]: the size for values of type `[integer]` cannot be
known at compilation time at the line `for n in numbers[1..] {`
I did some searching around, but found nothing.
fn main()
let mut numbers = Vec::new();
numbers.push(1);
numbers.push(32);
numbers.push(43);
numbers.push(42);
// ... And many more
println!(":?", numbers); // Sanity
let mut sum = 0;
// Problem code area
for n in numbers[1..]
sum = sum + n;
// Problem code area
println!("", sum);
Also, the problem lines work if I replace them with following (adding &
and *
for ownership/borrowing and dereferencing)
for n in &numbers[1..]
sum = sum + *n;
Why is the compilation failing in the former way?
rust
This question already has an answer here:
What does “Sized is not implemented” mean?
2 answers
What is the difference between a slice and an array?
2 answers
How can I fix the error E0277: the trait bound `[usize]: std::marker::Sized` is not satisfied?
2 answers
In the following code I am getting the error:
error[E0277]: the size for values of type `[integer]` cannot be
known at compilation time at the line `for n in numbers[1..] {`
I did some searching around, but found nothing.
fn main()
let mut numbers = Vec::new();
numbers.push(1);
numbers.push(32);
numbers.push(43);
numbers.push(42);
// ... And many more
println!(":?", numbers); // Sanity
let mut sum = 0;
// Problem code area
for n in numbers[1..]
sum = sum + n;
// Problem code area
println!("", sum);
Also, the problem lines work if I replace them with following (adding &
and *
for ownership/borrowing and dereferencing)
for n in &numbers[1..]
sum = sum + *n;
Why is the compilation failing in the former way?
This question already has an answer here:
What does “Sized is not implemented” mean?
2 answers
What is the difference between a slice and an array?
2 answers
How can I fix the error E0277: the trait bound `[usize]: std::marker::Sized` is not satisfied?
2 answers
rust
rust
edited Nov 10 at 14:38
Shepmaster
147k12282416
147k12282416
asked Nov 10 at 5:58
Sid
3,024103681
3,024103681
marked as duplicate by hellow, Shepmaster
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 10 at 14:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by hellow, Shepmaster
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 10 at 14:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I don't get your question, because you already came up with an solution. What is your concern?
– hellow
Nov 10 at 7:30
Also @hellow, the error message you posted is different. It's for the same error code, but not related. The message posted above is full message. Did you try running the code? You can do it here: play.rust-lang.org
– Sid
Nov 10 at 10:02
Please don't accuse anyone on downvoting or voting for close your question. First, I did not downvote it, second it you should look at the full error message! As I posted (it's gone?!) the error message is telling you what to do.
– hellow
Nov 10 at 14:05
2
Sid, when you ask a question and there are some comments, e.g. recommendations or questions regarding the question, then we ask them. When we think, that the question already has been answered, we mark it as an duplicate. It doesn't mean, that it is necessarily a bad question, just that it has been answered. We are happy to help you, when your questions are good, which means they are clear and have a good mcve (when it is about a programming problem). Please read the info page for rust.
– hellow
Nov 10 at 15:13
add a comment |
I don't get your question, because you already came up with an solution. What is your concern?
– hellow
Nov 10 at 7:30
Also @hellow, the error message you posted is different. It's for the same error code, but not related. The message posted above is full message. Did you try running the code? You can do it here: play.rust-lang.org
– Sid
Nov 10 at 10:02
Please don't accuse anyone on downvoting or voting for close your question. First, I did not downvote it, second it you should look at the full error message! As I posted (it's gone?!) the error message is telling you what to do.
– hellow
Nov 10 at 14:05
2
Sid, when you ask a question and there are some comments, e.g. recommendations or questions regarding the question, then we ask them. When we think, that the question already has been answered, we mark it as an duplicate. It doesn't mean, that it is necessarily a bad question, just that it has been answered. We are happy to help you, when your questions are good, which means they are clear and have a good mcve (when it is about a programming problem). Please read the info page for rust.
– hellow
Nov 10 at 15:13
I don't get your question, because you already came up with an solution. What is your concern?
– hellow
Nov 10 at 7:30
I don't get your question, because you already came up with an solution. What is your concern?
– hellow
Nov 10 at 7:30
Also @hellow, the error message you posted is different. It's for the same error code, but not related. The message posted above is full message. Did you try running the code? You can do it here: play.rust-lang.org
– Sid
Nov 10 at 10:02
Also @hellow, the error message you posted is different. It's for the same error code, but not related. The message posted above is full message. Did you try running the code? You can do it here: play.rust-lang.org
– Sid
Nov 10 at 10:02
Please don't accuse anyone on downvoting or voting for close your question. First, I did not downvote it, second it you should look at the full error message! As I posted (it's gone?!) the error message is telling you what to do.
– hellow
Nov 10 at 14:05
Please don't accuse anyone on downvoting or voting for close your question. First, I did not downvote it, second it you should look at the full error message! As I posted (it's gone?!) the error message is telling you what to do.
– hellow
Nov 10 at 14:05
2
2
Sid, when you ask a question and there are some comments, e.g. recommendations or questions regarding the question, then we ask them. When we think, that the question already has been answered, we mark it as an duplicate. It doesn't mean, that it is necessarily a bad question, just that it has been answered. We are happy to help you, when your questions are good, which means they are clear and have a good mcve (when it is about a programming problem). Please read the info page for rust.
– hellow
Nov 10 at 15:13
Sid, when you ask a question and there are some comments, e.g. recommendations or questions regarding the question, then we ask them. When we think, that the question already has been answered, we mark it as an duplicate. It doesn't mean, that it is necessarily a bad question, just that it has been answered. We are happy to help you, when your questions are good, which means they are clear and have a good mcve (when it is about a programming problem). Please read the info page for rust.
– hellow
Nov 10 at 15:13
add a comment |
1 Answer
1
active
oldest
votes
numbers[1..]
doesn't have a size because it is a bare¹ slice of numbers
. Slices can only be used behind some kind of pointer, e.g. &[T]
, Box<[T]>
, or Arc<[T]>
. The following questions have more information about slices:
- What is the difference between a slice and an array?
- How can I fix the error E0277: the trait bound `[usize]: std::marker::Sized` is not satisfied?
Because it is unsized, [T]
cannot implement IntoIterator
, which is the trait used for iterating over things in for
loops. (See Why can I iterate over a slice twice, but not a vector?) IntoIterator::into_iter
has the following prototype:
fn into_iter(self) -> Self::IntoIter;
Self
cannot be an unsized type because into_iter
takes self
by value.
&numbers[1..]
is a reference (type &[T]
), which both has a size and implements IntoIterator
, so it works fine.
The full compiler output also gives an error message about IntoIterator
, and helpfully suggests using numbers[1..].iter()
instead. Because .iter()
takes &self
, it can be called on an unsized type, so this is another way to fix the problem.
¹In many places, including the official documentation, the unqualified term "slice" is used to refer to the reference type &[T]
. I call numbers[1..]
a bare slice because it is not a reference; its type is just [T]
.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
numbers[1..]
doesn't have a size because it is a bare¹ slice of numbers
. Slices can only be used behind some kind of pointer, e.g. &[T]
, Box<[T]>
, or Arc<[T]>
. The following questions have more information about slices:
- What is the difference between a slice and an array?
- How can I fix the error E0277: the trait bound `[usize]: std::marker::Sized` is not satisfied?
Because it is unsized, [T]
cannot implement IntoIterator
, which is the trait used for iterating over things in for
loops. (See Why can I iterate over a slice twice, but not a vector?) IntoIterator::into_iter
has the following prototype:
fn into_iter(self) -> Self::IntoIter;
Self
cannot be an unsized type because into_iter
takes self
by value.
&numbers[1..]
is a reference (type &[T]
), which both has a size and implements IntoIterator
, so it works fine.
The full compiler output also gives an error message about IntoIterator
, and helpfully suggests using numbers[1..].iter()
instead. Because .iter()
takes &self
, it can be called on an unsized type, so this is another way to fix the problem.
¹In many places, including the official documentation, the unqualified term "slice" is used to refer to the reference type &[T]
. I call numbers[1..]
a bare slice because it is not a reference; its type is just [T]
.
add a comment |
numbers[1..]
doesn't have a size because it is a bare¹ slice of numbers
. Slices can only be used behind some kind of pointer, e.g. &[T]
, Box<[T]>
, or Arc<[T]>
. The following questions have more information about slices:
- What is the difference between a slice and an array?
- How can I fix the error E0277: the trait bound `[usize]: std::marker::Sized` is not satisfied?
Because it is unsized, [T]
cannot implement IntoIterator
, which is the trait used for iterating over things in for
loops. (See Why can I iterate over a slice twice, but not a vector?) IntoIterator::into_iter
has the following prototype:
fn into_iter(self) -> Self::IntoIter;
Self
cannot be an unsized type because into_iter
takes self
by value.
&numbers[1..]
is a reference (type &[T]
), which both has a size and implements IntoIterator
, so it works fine.
The full compiler output also gives an error message about IntoIterator
, and helpfully suggests using numbers[1..].iter()
instead. Because .iter()
takes &self
, it can be called on an unsized type, so this is another way to fix the problem.
¹In many places, including the official documentation, the unqualified term "slice" is used to refer to the reference type &[T]
. I call numbers[1..]
a bare slice because it is not a reference; its type is just [T]
.
add a comment |
numbers[1..]
doesn't have a size because it is a bare¹ slice of numbers
. Slices can only be used behind some kind of pointer, e.g. &[T]
, Box<[T]>
, or Arc<[T]>
. The following questions have more information about slices:
- What is the difference between a slice and an array?
- How can I fix the error E0277: the trait bound `[usize]: std::marker::Sized` is not satisfied?
Because it is unsized, [T]
cannot implement IntoIterator
, which is the trait used for iterating over things in for
loops. (See Why can I iterate over a slice twice, but not a vector?) IntoIterator::into_iter
has the following prototype:
fn into_iter(self) -> Self::IntoIter;
Self
cannot be an unsized type because into_iter
takes self
by value.
&numbers[1..]
is a reference (type &[T]
), which both has a size and implements IntoIterator
, so it works fine.
The full compiler output also gives an error message about IntoIterator
, and helpfully suggests using numbers[1..].iter()
instead. Because .iter()
takes &self
, it can be called on an unsized type, so this is another way to fix the problem.
¹In many places, including the official documentation, the unqualified term "slice" is used to refer to the reference type &[T]
. I call numbers[1..]
a bare slice because it is not a reference; its type is just [T]
.
numbers[1..]
doesn't have a size because it is a bare¹ slice of numbers
. Slices can only be used behind some kind of pointer, e.g. &[T]
, Box<[T]>
, or Arc<[T]>
. The following questions have more information about slices:
- What is the difference between a slice and an array?
- How can I fix the error E0277: the trait bound `[usize]: std::marker::Sized` is not satisfied?
Because it is unsized, [T]
cannot implement IntoIterator
, which is the trait used for iterating over things in for
loops. (See Why can I iterate over a slice twice, but not a vector?) IntoIterator::into_iter
has the following prototype:
fn into_iter(self) -> Self::IntoIter;
Self
cannot be an unsized type because into_iter
takes self
by value.
&numbers[1..]
is a reference (type &[T]
), which both has a size and implements IntoIterator
, so it works fine.
The full compiler output also gives an error message about IntoIterator
, and helpfully suggests using numbers[1..].iter()
instead. Because .iter()
takes &self
, it can be called on an unsized type, so this is another way to fix the problem.
¹In many places, including the official documentation, the unqualified term "slice" is used to refer to the reference type &[T]
. I call numbers[1..]
a bare slice because it is not a reference; its type is just [T]
.
answered Nov 10 at 13:52
trentcl
6,35231234
6,35231234
add a comment |
add a comment |
I don't get your question, because you already came up with an solution. What is your concern?
– hellow
Nov 10 at 7:30
Also @hellow, the error message you posted is different. It's for the same error code, but not related. The message posted above is full message. Did you try running the code? You can do it here: play.rust-lang.org
– Sid
Nov 10 at 10:02
Please don't accuse anyone on downvoting or voting for close your question. First, I did not downvote it, second it you should look at the full error message! As I posted (it's gone?!) the error message is telling you what to do.
– hellow
Nov 10 at 14:05
2
Sid, when you ask a question and there are some comments, e.g. recommendations or questions regarding the question, then we ask them. When we think, that the question already has been answered, we mark it as an duplicate. It doesn't mean, that it is necessarily a bad question, just that it has been answered. We are happy to help you, when your questions are good, which means they are clear and have a good mcve (when it is about a programming problem). Please read the info page for rust.
– hellow
Nov 10 at 15:13